18 Sept 2024

Union Budget 2024-25 – All You Need to Know

 

Union Budget 2024-25 – All You Need to Know

Here, each letter represents a key focus area: Employment, MSMEs, Productivity, Land, Opportunity, Youth, Middle Class, Energy Security, New Generation Reforms, and Technology. These ten priorities outline the government’s strategy for economic growth and development.

Budget 2024 Key Highlights

Below are the major announcements from the Union Budget 2024:

  • Income Tax Changes: Introduction of new income tax slabs and an increase in the standard deduction.
  • Social Security: Enhancements to the National Pension Scheme (NPS) deductions.
  • Sectoral Focus: Emphasis on agriculture, MSMEs, and employment generation.

Taxation Changes

The budget introduces several changes to the income tax regime. So, here are the revised tax slabs under the New Tax Regime:

New Tax Slabs
  • Up to ₹3 lakh: 0%
  • ₹3 lakh to ₹7 lakh: 5%
  • ₹7 lakh to ₹10 lakh: 10%
  • ₹10 lakh to ₹12 lakh: 15%
  • ₹12 lakh to ₹15 lakh: 20%
  • Above ₹15 lakh: 30%

With this new regime, salaried employees can save up to ₹17,500/- in income tax.

  • Income Tax Standard Deduction: Increased from ₹50,000 to ₹75,000 to provide more relief to salaried employees.
  • Long Term Capital Gains Tax: LTCG gains tax hiked from 10% to 12.5%.

Social Security and Employment Benefits

The budget also introduces several measures to enhance social security and employment:

  • Increased NPS Deductions: Employers’ contributions to NPS have been increased from 10% to 14% of the employee’s salary.
  • One-Month Wage for New Entrants: The government will provide a one-month wage to all new employees in the formal sector in 3 instalments up to ₹15,000. This would directly be transferred as part of their provident fund contribution.
  • Family Pension Deduction: The deduction on family pensions has been increased from ₹15,000 to ₹25,000, thereby, providing better financial stability to pensioners.
Budget 2024 Highlights

Impact On Major Sectors and Funds Allocation

Different sectors are set to benefit from various budget allocations:

  • Agriculture and Allied Sectors: ₹1.52 lakh crores have been allocated to enhance strength and productivity in the agricultural sector.
  • Education: For education and employment, a total of ₹1.48 lakh crores has been allocated.
  • Healthcare: Funds of approximately ₹90,958 crores are allocated for building healthcare facilities to improve public services.
  • Infrastructure: To maintain and increase infrastructural development, ₹11.11 lakh crores have been allocated in the Union Budget 2024.
  • Urban Development: For upgrading urban infrastructure, ₹82,575 crores have been allocated by the government.
  • Technology and Innovation: A total of ₹21,936 crores has been allocated to the Ministry of Electronics and Information Technology to support startups and technology companies.

Revenue and Expenditure

The budget outlines the government’s revenue and expenditure plans:

  • Revenue Forgone: Approximately ₹37,000 crore in revenue will be forgone due to the new tax changes, with an additional ₹30,000 crore to be mobilised.
  • Expenditure Plans: The government is set to spend ₹48,20,512 crore in 2024-25, which is 8.5% more than the actual expenditure in 2023-24. The major expenditure areas include agriculture, infrastructure, social welfare programs, and employment schemes.

Economic Projections and Targets

  • GDP Growth: The budget highlights the expectation for continued strong economic performance.
  • Fiscal Deficit: A target set at 4.9% for the fiscal deficit with plans to reduce it to 4.5%.

MSMEs and Employment Generation

The budget also looks forward to supporting Micro, Small, and Medium Enterprises (MSMEs) and generating employment. As a result, it doubles the Mudra loan limits to ₹20 lakh and implements a digital credit assessment model to nurture growth in the MSME sector.

Impact On Gold Price

As per the Budget 2024, the import duties on gold have been reduced from 15% to 6%. This custom duties cut will reduce the taxes on gold from 18.5% to 9%, eventually, reducing the price of gold.

Conclusion

So, this sums up the basics of the Budget 2024 key updates.

Now, if you are looking for instant personal loan to manage your expenses, the  app is a great choice. It offers quick and easy personal loans with minimal documentation and is perfect for emergencies, home renovations, or other personal expenses.

Moreover, with the reduction in import duties on gold, you can consider investing in digital gold with and have a hedge against inflation. Download the app now!

How to Get a Personal Loan Without ITR?

 

How to Get a Personal Loan Without ITR?

What is an ITR?

An Income Tax Return (ITR) is a form that you file with the government to report your income for the year. It is a way to show how much money you earned and whether you paid the right amount of taxes. Personal loans on ITR are quite common as lenders often ask for ITR to confirm if you have a steady income and can repay a loan.

Importance of ITR

ITR shows your income over the past financial year and hence, it is proof that you have a regular income. It makes it easier to get a loan, especially if you are asking for a large amount. Having an ITR would help you get lower interest rates as it shows you are a low-risk borrower. Filing ITR also allows you to claim tax deductions, and if you file it regularly, then it would boost your credit score.

Scenarios Where You Might Not Have an ITR

  • Freelancers or small business owners may not file ITR if their income is below the taxable limit.
  • If you just started working, you might not have filed an ITR yet.
  • If you are currently not working, you might not have an ITR.
  • Senior citizens living off their savings might not need to file an ITR.
Personal loan without ITR

Alternative Documents and Proofs to Secure a Personal Loan Without ITR

Here are the documents required for a personal loan without ITR:

  • Bank statements for the past 6-12 months
  • Recent pay slips from your job
  • Form 16 from your employer
  • GST returns for business owners
  • Rental income proof
  • Property documents, if you own a property
  • Investment proof
  • An excellent credit score

Types of Personal Loans Available Without ITR

  • Secured loans where you provide something valuable like gold or property as security
  • Instant personal loans from apps 
  • Loans secured by your investments such as shares
  • Loans based on your salary slips and bank statements
  • Loans from Non-Banking Financial Companies
  • Loans against salary account

Tips for Securing a Personal Loan Without ITR

  • Always keep an excellent credit score
  • Use bank statements, salary slips, or other documents to show income
  • Offer something valuable as security can help you get a loan
  • Choose the right lender like CASHe
  • Keep your existing loans and debts manageable
  • Start with a smaller loan amount, If it is your first time borrowing without ITR

Conclusion

This is how you can get a personal loan without ITR. Now, if you are looking for an instant personal loan without income proof and with competitive interest rates, then download the CASHe app. CASHe offers instant personal loans without ITR and provides a simple and fast way to apply for a loan with minimal paperwork. So, wait no further, and explore instant personal loan options today!

Documents required for quick loan

 

Documents required for quick loan

When you need quick funds, either for emergencies or last-minute expenses, a personal loan can be a lifesaver. However, before you can get approved for a quick loan, you need to ensure that you have all the necessary documents in order. In this guide, we will walk you through the essential documents required for quick loans and provide tips on how to prepare your documents efficiently.,/p>

Understanding Quick Loans

Quick loans are very much like personal loans but they can be availed in a shorter period and get capped around ₹3,00,000-5,00,000 by most financial institutions. They are a type of unsecured loan and are also known as instant loans in the market.

These loans are typically processed quickly, making them an ideal option for those in urgent need of money. To apply for a quick loan, you need to provide specific documents to the lender to support your loan application.

Essential Documents Required for Quick Loans

Quick Loan Documents

Like instant personal loans, quick loans also need mandatory submission of certain documents that will help the financial institution gauge your capacity to repay your loans and understand your overall profile. You will have to provide the following documents to be eligible for a quick loan:

Identification Proof: You will need to provide a copy of your Aadhaar card, passport, driving licence, or voter ID card as proof of your identity.

Address Proof: Documents such as utility bills, rental agreement, or Aadhaar card can be submitted to verify your residential address.

Income Proof: Bank statements, salary slips, income tax returns, or Form 16 can serve as proof of your income and repayment capacity.

Employment Proof: If you are a salaried individual, you may need to provide your employment offer letter, employee ID card, or appointment letter from your employer.

Business Proof (for self-employed individuals): If you are self-employed, documents such as a business registration certificate, GST certificate, or partnership deed may be required.

Credit Score: While not a document, your credit score plays a crucial role in determining your loan eligibility. Make sure to have a good credit score before applying for a quick loan.

How to Prepare Your Documents

Organize Your Documents: Create a checklist of all the required documents and ensure that you have them handy before applying for a quick loan.

Verify Document Validity: Check the validity of your identification and address proofs to ensure that they are up to date.

Maintain Clarity: Ensure that all documents are legible and free of any errors or alterations.

Seek Professional Help: If you are unsure about any document requirements, consider seeking assistance from a financial advisor or loan officer.


Conclusion

The idea of quick loans is to get funds on an immediate basis and if you provide all the documents on time, your loan can get sanctioned within 24 hours.

If you are looking for Instant personal loans up to ₹3,00,000 then go for CASHe because their loan disbursal process gets done within 15-20 minutes. There are no restrictions on the usage of the funds, the disbursal is quick and you get flexible loan tenures that are aligned with your repayment capacity.

6 Sept 2024

Cloud Integration and Hybrid Data Solutions

 Cloud integration and hybrid data solutions involve combining on-premises infrastructure with cloud services to create flexible, scalable, and efficient data environments. This approach allows organizations to leverage the strengths of both local (on-premises) and cloud-based systems, enhancing performance, availability, and data management capabilities.

Here’s a deeper dive into Cloud Integration and Hybrid Data Solutions:

1. Hybrid Cloud Architecture

A hybrid cloud architecture combines private (on-premises) and public cloud environments. It allows data and applications to move between these environments, enabling:

  • Flexibility: Workloads can be managed dynamically across on-premises and cloud environments based on performance, cost, or security needs.
  • Cost Efficiency: You can keep sensitive or high-priority data on-premises while using the cloud for scalability and cost-effective storage.
  • Disaster Recovery & High Availability: Data can be replicated or backed up in the cloud, ensuring business continuity in case of an on-premises failure.

2. Data Integration Strategies

Organizations can integrate data from multiple sources (on-premises, cloud databases, applications, IoT devices, etc.) to create a unified data platform. Key methods include:

  • ETL/ELT: Extract, Transform, Load (ETL) or Extract, Load, Transform (ELT) pipelines move data between sources. ELT is more cloud-friendly as transformations occur after the data is loaded.
  • Data Replication: Continuous replication of on-premises data to the cloud ensures data synchronization, providing low-latency access across environments.
  • APIs: APIs connect various services, allowing applications in different environments to communicate seamlessly.

3. Cloud Data Services

Cloud platforms like Microsoft Azure, Amazon Web Services (AWS), and Google Cloud offer robust data solutions:

  • Azure Synapse Analytics: Combines big data and data warehousing, offering real-time analytics over large datasets, whether stored on-premises or in the cloud.
  • AWS Glue: Serverless data integration service that makes it easy to prepare data for analytics by combining data from various sources.
  • Google BigQuery: Serverless data warehouse with built-in machine learning, designed for handling vast amounts of data across hybrid environments.

4. Multi-Cloud Strategy

Some organizations adopt a multi-cloud approach, using services from more than one cloud provider to avoid vendor lock-in, optimize costs, and improve redundancy. A well-executed multi-cloud strategy offers:

  • Interoperability: Data and services work seamlessly across different cloud providers.
  • Data Portability: Simplified movement of workloads between different cloud environments.
  • Compliance & Regulation: Certain clouds may be chosen for specific workloads based on regional compliance or data residency requirements.

5. Edge Computing

Edge computing complements cloud and hybrid models by processing data closer to its source (e.g., IoT devices or local servers). This reduces latency and bandwidth costs, especially when processing time-sensitive data.

  • Hybrid Edge Architecture: Combines edge computing with cloud services, sending processed data to the cloud for storage or further analysis while keeping latency-critical operations local.
  • Use Cases: Real-time monitoring, predictive maintenance, and industrial automation.

6. Data Virtualization

Data virtualization allows for real-time access and query capabilities across diverse data sources without physically moving data. This reduces complexity in hybrid cloud scenarios, providing:

  • Unified Data View: Access and manipulate data from multiple sources (cloud, on-premises, external) without duplication.
  • Real-Time Analytics: Execute analytics directly on distributed datasets without the need for extensive ETL processes.

7. Security and Governance in Hybrid Solutions

Security remains a critical concern in hybrid solutions. Organizations need to implement:

  • Data Encryption: Data must be encrypted both in transit (between environments) and at rest.
  • Identity & Access Management (IAM): Ensure proper authentication and role-based access control (RBAC) for users across both cloud and on-premises environments.
  • Compliance: Hybrid solutions need to meet regulatory standards (e.g., GDPR, HIPAA), especially when moving sensitive data between environments.

8. Cloud-Native Technologies in Hybrid Solutions

  • Containers & Kubernetes: Kubernetes orchestrates containers across hybrid cloud environments, enabling portability and consistency in application deployment.
  • Serverless Functions: Services like AWS Lambda, Azure Functions, and Google Cloud Functions allow code to run without provisioning servers, providing scalable compute in hybrid setups.
  • Microservices Architecture: Enables the development of applications as small, independently deployable services. Microservices work well in hybrid environments, allowing specific services to run in the cloud while others remain on-premises.

9. Benefits of Hybrid Data Solutions

  • Scalability: Utilize the cloud to handle spikes in demand while maintaining core workloads on-premises.
  • Cost Control: Manage expenses by leveraging cloud resources dynamically and reducing dependency on expensive hardware.
  • Innovation & Agility: Experiment with new cloud services (like machine learning, AI, or advanced analytics) without disrupting core on-premises operations.
  • Data Sovereignty: Maintain control over sensitive data by keeping it on-premises while using the cloud for less critical data or compute-heavy tasks.

10. Real-World Use Cases

  • Healthcare: Hybrid solutions allow sensitive patient data to remain on-premises while analytics and machine learning run in the cloud.
  • Financial Services: Banks use hybrid architectures to comply with regulations that mandate data residency while leveraging cloud platforms for AI-driven risk assessment or fraud detection.
  • Manufacturing: Edge devices collect data on the factory floor, process it locally, and send summary data to the cloud for further analysis.

By combining the strengths of on-premises systems and cloud platforms, cloud integration and hybrid data solutions offer businesses a path to scalable, secure, and cost-effective data management, enabling them to handle modern workloads and adapt to evolving technology landscapes.

Intelligent Query Processing (IQP)

 Intelligent Query Processing (IQP) is a suite of features introduced in SQL Server to optimize query performance with minimal changes to existing code. It is designed to automatically improve query performance by adapting to runtime conditions, reducing the need for manual query tuning. IQP is part of the broader Intelligent Database concept and includes several enhancements aimed at improving the performance of queries across various workloads.

Here are the key features of Intelligent Query Processing:

1. Batch Mode on Rowstore (SQL Server 2019)

  • Previously, batch mode execution was only available for columnstore indexes. Now, SQL Server 2019 extends batch mode processing to rowstore tables, providing performance improvements for analytical queries on traditional rowstore tables.

2. Table Variable Deferred Compilation (SQL Server 2019)

  • In prior versions, table variables were always assumed to have a single row at compile time, leading to suboptimal query plans when they contained more data. SQL Server 2019 defers the compilation of table variables until runtime when actual row counts are known, improving query performance.

3. Approximate Query Processing (SQL Server 2019)

  • The APPROX_COUNT_DISTINCT function is introduced to provide faster approximate distinct count calculations for large datasets. This is useful in cases where exact precision is not required but performance is critical, such as in large analytical workloads.

4. Scalar UDF Inlining (SQL Server 2019)

  • Scalar user-defined functions (UDFs) often caused performance bottlenecks because they were executed row-by-row. With inlining, SQL Server replaces the UDF calls with the function body, allowing the entire query to be optimized as a single unit and improving performance significantly.

5. Adaptive Joins (SQL Server 2017)

  • SQL Server can now switch dynamically between Nested Loops and Hash Joins during query execution based on the actual row count. This helps optimize join performance in cases where cardinality estimates may be inaccurate.

6. Interleaved Execution for Multi-Statement Table-Valued Functions (SQL Server 2017)

  • SQL Server can now defer execution of certain parts of the query plan involving multi-statement table-valued functions (MSTVF) until runtime when actual data sizes are known, leading to more accurate query plans and better performance.

7. Memory Grant Feedback (SQL Server 2017)

  • SQL Server adjusts the memory allocated for query execution based on feedback from prior executions. If a query requests too much or too little memory, the system learns and adjusts the memory grant for future executions, reducing memory contention and improving performance.

8. Batch Mode Memory Grant Feedback (SQL Server 2019)

  • Expanding on memory grant feedback, batch mode execution can now dynamically adjust memory grants, which is especially beneficial for large analytical queries, helping to avoid both under- and over-provisioning of memory.

9. Batch Mode Adaptive Joins (SQL Server 2019)

  • Similar to adaptive joins, this feature extends adaptive behavior to batch mode, allowing the system to switch between join strategies at runtime based on data distribution and actual row counts.

10. Cardinality Estimation Feedback (SQL Server 2022)

  • Introduced in SQL Server 2022, this feature helps improve the accuracy of query plans by updating cardinality estimates based on feedback from prior executions, optimizing for future runs.

11. Parameter Sensitivity Plan Optimization (SQL Server 2022)

  • Parameter sensitivity issues can cause performance problems due to a single query plan being used for different parameter values. This feature addresses the problem by generating and storing multiple plans for a single query based on parameter values, improving query performance and reducing bottlenecks.

12. Degree of Parallelism (DOP) Feedback (SQL Server 2022)

  • This feature optimizes the degree of parallelism used by queries, adjusting based on the actual runtime conditions. Over time, SQL Server learns and refines the parallelism strategy, improving the query's performance in future executions.

These IQP features help SQL Server adapt to changing workloads and data distribution, reducing manual tuning effort while improving performance across OLTP (Online Transaction Processing) and OLAP (Online Analytical Processing) workloads. They contribute to more efficient query execution by learning from actual data and workload behavior.

Query Store Enhancements

 Query Store in SQL Server has received several enhancements in recent versions, making it even more powerful for performance monitoring and query tuning. Here are some of the key enhancements:

1. Query Store for Read-Only Replicas (SQL Server 2019)

  • Query Store now supports capturing performance data on read-only replicas in Always On Availability Groups. This allows you to monitor and tune queries running on these replicas without impacting the primary.

2. Custom Capture Policies (SQL Server 2019)

  • You can define custom capture policies for Query Store, which allows you to control which queries are captured based on factors such as execution frequency, resource consumption, or duration. This helps reduce overhead by ignoring less important queries.

3. Automatic Plan Correction (SQL Server 2017+)

  • SQL Server can automatically detect and correct query performance regressions by reverting to an older query plan that is known to perform better. This is part of the Intelligent Query Processing (IQP) feature set.

4. Improved User Interface in SSMS

  • The Query Store UI in SQL Server Management Studio (SSMS) has improved significantly, offering better visualization for comparing query performance over time, analyzing plan changes, and more.

5. Hybrid Workloads and Query Store for Cloud Environments

  • Azure SQL Database and SQL Managed Instance also benefit from Query Store enhancements, such as the ability to monitor query performance across different environments (on-premises and cloud) and integrate with Azure monitoring tools.

6. Wait Statistics

  • Query Store now includes wait statistics, which help you understand the causes of performance bottlenecks related to waits (e.g., I/O, locks, CPU). This provides better insight into the actual root causes of performance issues.

7. Aggregated Query Data

  • Query Store now offers enhanced options for aggregating query data, making it easier to analyze query patterns and performance over longer periods of time without needing to focus on individual executions.

8. Query Store on a Per-Database Basis

  • You can now enable or disable Query Store at the database level, providing more control over where Query Store is used and reducing unnecessary overhead in less critical databases.

These enhancements help developers and DBAs monitor, troubleshoot, and optimize query performance more efficiently across various environments.

2 Sept 2024

10 What is the difference between decode and case?

 DECODE and CASE are both conditional expressions used in SQL to perform conditional logic within queries. However, they differ in syntax, functionality, and availability across different database systems. Here's a breakdown of the differences:

1. Syntax

  • DECODE Syntax:



    DECODE(expression, search1, result1, search2, result2, ..., default)
    • expression: The value to be compared.
    • search: The value to compare against the expression.
    • result: The value to return if the search value matches the expression.
    • default: The value to return if none of the search values match the expression.
  • CASE Syntax:

    • Simple CASE Expression:

      CASE expression WHEN value1 THEN result1 WHEN value2 THEN result2 ... ELSE default END
    • Searched CASE Expression:

      CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE default END

2. Functionality

  • DECODE:

    • Acts like a simplified CASE expression.
    • Only compares a single expression against a series of possible values.
    • Stops evaluating once a match is found.
    • More compact and concise for simple equality checks.
    • Primarily available in Oracle databases.
  • CASE:

    • More powerful and flexible than DECODE.
    • Can handle multiple conditions and logical expressions, not just equality checks.
    • Available in almost all major relational database management systems (RDBMS), including Oracle, SQL Server, MySQL, and PostgreSQL.
    • Provides both a simple case (for equality checks) and a searched case (for more complex conditions).

3. Portability

  • DECODE:

    • Mostly specific to Oracle databases, so it is less portable across different RDBMS.
    • Not natively supported in SQL Server, MySQL, or PostgreSQL.
  • CASE:

    • Standard SQL and widely supported across different RDBMS.
    • More portable, making it a better choice for SQL scripts that need to work across different database systems.

4. Use Cases

  • DECODE:

    • Best for simple mappings and when working within Oracle.
    • Example:

      SELECT DECODE(status, 'A', 'Active', 'I', 'Inactive', 'Unknown') FROM users;
  • CASE:

    • Preferred for complex conditions, non-Oracle databases, and where portability is a concern.
    • Example:

      SELECT CASE WHEN status = 'A' THEN 'Active' WHEN status = 'I' THEN 'Inactive' ELSE 'Unknown' END FROM users;

5. Performance

  • Performance is typically comparable between the two for simple use cases, but CASE can be more optimized and versatile for complex scenarios.

Conclusion:

  • Use DECODE if you are working in Oracle and need a quick, simple mapping solution.
  • Use CASE for more complex conditions, for better portability, and when working with non-Oracle databases.

29 Aug 2024

Parameter sniffing in SQL Server

 Parameter sniffing in SQL Server is a feature where the query optimizer uses the specific parameter values passed to a stored procedure or query to generate an execution plan. While this can be beneficial for performance, it can also cause issues when the chosen execution plan is not optimal for other parameter values. This can lead to queries performing poorly for some inputs, particularly when the data distribution is uneven.

Why Parameter Sniffing Can Be a Problem

When SQL Server compiles a stored procedure or a parameterized query for the first time, it creates an execution plan based on the initial parameter values provided. If those initial values are atypical or represent edge cases, the generated plan might not perform well for more common parameter values.

How to Resolve Parameter Sniffing Issues

  1. Use OPTION (RECOMPILE)

    • Adding OPTION (RECOMPILE) to a query forces SQL Server to generate a new execution plan every time the query is executed.
    • Pros: Ensures the plan is optimized for the specific parameter values at runtime.
    • Cons: Recompiling the plan for every execution can add overhead, especially for frequently run queries.


    SELECT * FROM Orders WHERE CustomerID = @CustomerID OPTION (RECOMPILE);
  2. Use WITH RECOMPILE in Stored Procedures

    • Adding WITH RECOMPILE when creating or executing a stored procedure forces SQL Server to recompile the procedure each time it is executed.
    • Pros: Ensures the execution plan is tailored to the specific parameters each time.
    • Cons: Similar to OPTION (RECOMPILE), this can introduce overhead.


    CREATE PROCEDURE GetOrders @CustomerID INT WITH RECOMPILE AS BEGIN SELECT * FROM Orders WHERE CustomerID = @CustomerID; END;
  3. Optimize with OPTION (OPTIMIZE FOR @parameter)

    • Use the OPTIMIZE FOR hint to instruct SQL Server to optimize the query for a specific parameter value, which might be more representative of typical use cases.
    • Pros: Can lead to a more consistent execution plan for typical cases.
    • Cons: May still be suboptimal for other edge cases.

    SELECT * FROM Orders WHERE CustomerID = @CustomerID OPTION (OPTIMIZE FOR (@CustomerID = 123));
  4. Use OPTIMIZE FOR UNKNOWN

    • This option tells SQL Server to generate a "generalized" execution plan rather than one based on the specific parameter values, as if the parameter values were not known at compile time.
    • Pros: Useful when you want a more generic plan that doesn't overly favor any particular parameter value.
    • Cons: The resulting plan might not be optimal for any specific case but can provide more stable performance across a range of values.

    SELECT * FROM Orders WHERE CustomerID = @CustomerID OPTION (OPTIMIZE FOR UNKNOWN);
  5. Manually Create Multiple Plans with Different Parameters

    • You can create separate stored procedures or queries optimized for different parameter ranges.
    • Pros: Each version can be tailored to a specific type of query or set of parameter values.
    • Cons: Increases maintenance complexity as you manage multiple versions of the same logic.

    IF @CustomerID BETWEEN 1 AND 100 BEGIN EXEC GetOrders_SmallCustomers @CustomerID; END ELSE BEGIN EXEC GetOrders_LargeCustomers @CustomerID; END;
  6. Use Dynamic SQL

    • Writing your query using dynamic SQL inside a stored procedure ensures the query plan is compiled fresh for each execution based on the actual parameter values.
    • Pros: Tailors the execution plan to the exact values being passed.
    • Cons: Dynamic SQL can make code harder to read and maintain and may have security implications (e.g., SQL injection risks).
    DECLARE @SQL NVARCHAR(MAX); SET @SQL = 'SELECT * FROM Orders WHERE CustomerID = @CustomerID'; EXEC sp_executesql @SQL, N'@CustomerID INT', @CustomerID;
  7. Index Tuning

    • Sometimes, parameter sniffing issues are exacerbated by suboptimal indexes. Reviewing and optimizing indexes can mitigate these issues.
    • Pros: Can resolve the root cause by ensuring the most efficient data access methods.
    • Cons: Requires analysis and might involve significant changes to the indexing strategy.

Monitoring and Diagnosing Parameter Sniffing

  • Query Store: SQL Server's Query Store feature can help identify queries that suffer from parameter sniffing by tracking query performance and execution plans over time.
  • Execution Plan Analysis: Comparing execution plans for different parameter values can reveal if parameter sniffing is causing suboptimal plans.

By applying these strategies, you can manage and mitigate the effects of parameter sniffing, leading to more consistent and reliable query performance in SQL Server.

26 Aug 2024

SQL EXISTS vs IN vs JOIN Performance Comparison

 

Problem

I built a query that utilizes a subquery that needs to be compared back to a main query. I want to know how to best achieve this task. Should I use an IN statement? An EXISTS? Or maybe a JOIN? I need to know which options will be valid for my use case and which one will perform the best. I also need to be able to prove it.

Solution

As with many situations within SQL Server the answer depends on the circumstances. This tip will look at the pros and cons of each method and use a repeatable methodology to determine which method will offer the fastest performance. The best part about this tip is that the performance comparison methodology can be applied to any TSQL coding situation!

Compare SQL Server EXISTS vs. IN vs JOIN T-SQL Subquery Code

All of the demos in this tip will use the WideWorldImporters sample database which can be downloaded for free from here and will be run against SQL Server 2019. The images might be different, but the methodology should still work on older versions of SQL Server.

The subquery to be used will be a list of the top 3 sales people for a single quarter based on invoice count. For the simplicity of the example queries, this subquery will be stored as a view. As seen in this tip, for a simple query like this one, there likely isn't a difference in performance between using a view, CTE, or traditional subquery.

CREATE VIEW vTop3SPs2013Q2
AS
SELECT TOP 3 SalespersonPersonID
FROM Sales.Invoices
WHERE InvoiceDate BETWEEN '4/1/2013' AND '6/30/2013'
GROUP BY [SalespersonPersonID]
ORDER BY COUNT(*) DESC

SQL IN Code

The IN statement can be used to find rows in a query where one column can be matched to a value in a list of values. The list of values can be hard coded as a comma-separated list or can come from a subquery as it does in this example.

IN statements are easy to write and understand. The only downside is that they can only compare a single column from the subquery to a single column from the main query. If 2 or more values need to be compared then the IN statement cannot be used.

Below is a query that returns some invoices that belonged to our top group of salespeople. Notice that the subquery returns exactly one row. This is a requirement for use of the IN statement. Also note that the query inside the parentheses is a fully functional query on its own. It can be highlighted and executed by itself.

SELECT Invoices.InvoiceID, Invoices.TotalDryItems, People.FullName
FROM Sales.Invoices
  INNER JOIN [Application].People ON Invoices.SalespersonPersonID = People.PersonID
WHERE SalespersonPersonID IN (SELECT SalespersonPersonID FROM vTop3SPs2013Q2)
  AND InvoiceDate BETWEEN '4/1/2013' AND '6/30/2013'
  AND TotalDryItems >= 4;

Executing that query with both STATISTICS IO and STATISTIC TIME enabled, outputs this information. This output will give us some metrics for performance to compare to the other options. If unfamiliar with how to get this output, please consult this tip.

This screenshot of the output shows that the query returned 681 rows and used 13,116 reads from Invoices and 6 reads from People.  It executed in 39ms.

SQL EXISTS Code

The EXISTS statement functions similarly to the IN statement except that it can be used to find rows where one or more columns from the query can be found in another data set, usually a subquery. Hard coding isn't an option with EXISTS.

Below is the same query as above except that the IN has been replaced by EXISTS. The format for EXISTS and its subquery is a little bit different than for IN. In this case, the subquery references a column, I.SalespersonPersonID, that does seem to be available to the subquery. For this reason, the subquery cannot be executed on its own and can only be executed in the context of the entire query. This can sometimes be difficult to understand.

Logically, think of it as having the subquery run once for every row in the main query to be determined if a row exists. If a row exists upon executing the subquery, then the Boolean return value is true. Otherwise, it is false. The selected column(s) of the subquery does not matter as the result is tied only to the existence or non-existence of a row based on the FROM/JOIN/WHERE clauses in the subquery.

SELECT I.InvoiceID, I.TotalDryItems, People.FullName
FROM Sales.Invoices I
  INNER JOIN [Application].People ON I.SalespersonPersonID = People.PersonID
WHERE EXISTS (SELECT 1 FROM vTop3SPs2013Q2 WHERE SalespersonPersonID = I.SalespersonPersonID)
  AND I.InvoiceDate BETWEEN '4/1/2013' AND '6/30/2013'
  AND I.TotalDryItems >= 4;

Executing this query returns the following statistical output which is virtually identical to the IN statement.

sql server execution

SQL INNER JOIN Code

A regular JOIN can be used to find matching values in a subquery. Like EXISTS, JOIN allows one or more columns to be used to find matches. Unlike EXISTS, JOIN isn't as confusing to implement. The downside to JOIN is that if the subquery has any identical rows based on the JOIN predicate, then the main query will repeat rows which could lead to invalid query outputs. Both IN and EXISTS will ignore duplicate values in a subquery. Take extra precaution when joining to a table in this fashion. In this example, the view will not return any duplicate SalespersonPersonID values, so it is a safe implementation of a JOIN.

SELECT I.InvoiceID, I.TotalDryItems, People.FullName
FROM Sales.Invoices I
  INNER JOIN [Application].People ON I.SalespersonPersonID = People.PersonID
  INNER JOIN vTop3SPs2013Q2 ON I.SalespersonPersonID = vTop3SPs2013Q2.SalespersonPersonID
WHERE InvoiceDate BETWEEN '4/1/2013' AND '6/30/2013'
  AND TotalDryItems >= 4;

Executing this query returns the following statistical output which is, once again, virtually identical to the IN and EXISTS statement versions of the query.

sql server execution time

Why are all of the statistics the same?

The statistics for each of these 3 options are virtually identical because the optimizer is compiling all 3 options into the same query plan. This can be seen by running all three queries together while viewing the actual execution plans. The screenshot below shows one plan, but the exact same plan appears in each of the 3 query options.

create nonclustered index

Each copy of the query plan shows a missing index recommendation. Acting on that recommendation and creating the index will modify the plans and query performance statistics. Will it modify them all the same way? Let's find out. First, make the index, then rerun the 3 queries.

CREATE NONCLUSTERED INDEX mssqltips ON [Sales].[Invoices] ([InvoiceDate],[TotalDryItems]) 
   INCLUDE ([InvoiceID],[SalespersonPersonID]);

Now execute all 3 statements together again. Something interesting happens. All 3 plans have changed from the versions they were before the index creation, but they are not identical this time. The IN and EXISTS got the same new plan, but the JOIN gets a different plan.

The plan for the IN and EXISTS used the new index twice and performed a SEEK on the People table.

nested loops

This plan was generated for the JOIN version of the query. It used the new index twice, but performed a SCAN on the people table.

index scan

Checking the IO and TIME statistics for the 3 queries shows identical statistics for the 2 queries that shared a plan, but improved statistics and execution time for the JOIN version. If this were a query getting ready to be promoted to production, going with the JOIN would probably be the best bet.

sql server execution times

Conclusion

This query is a great example that while the optimizer strives to treat each option the same, it won't always do that. Using this performance verification methodology, along with understanding the value and limitations of each query option, will allow the programmer to make the best choice in each situation.

Many Uses of SQL CASE Expression in a JOIN Clause

 

Problem

Have you ever heard: "Never place a CASE expression in a JOIN clause?" I know I have. It's one of those best practices handed down by the SQL elders of old. I'm all for following guidelines, but sometimes I like to know why I'm doing something. Not just because someone wrote a blog article 20 years ago and it's the first one that shows up in search results. Stay tuned if you're like me, and don't follow advice blindly.


Solution

In this tutorial, we'll look at the many uses of a CASE expression. Additionally, why would you use a CASE expression in a JOIN clause? Next, I'll explore a common alternative that may or may not provide better performance. Finally, we'll answer the questions like "Should you never use a CASE expression in a JOIN clause?" My answer might surprise you.

Exploring SQL CASE

You're missing out if you've never used a CASE expression. It's the next command to learn after SELECT *. You might describe it as the MacGyver of T-SQL. Microsoft defines CASE as an expression that evaluates a list of conditions and returns one of the multiple possible result expressions. Said another way: Tell SQL that if this condition is true, then return this other thing.

SELECT CASE
           WHEN 1 = 1 THEN
               1
           WHEN 2 = 2 THEN
               2
           WHEN 3 = 3 THEN
               3
           ELSE
               0
       END AS [My Favorite Number];

Before moving on, try and answer the questions in the example above. What results will SQL Server return and why?

If you feel extra motivation, you can nest CASE expressions for added control. I'm not a fan of doing this if it makes code harder to follow. I would rather repeat certain parts. I know there is an adage of DRY ("Don't Repeat Yourself."), which, for the most part, I agree with.

Below is an example of a nested CASE expression. Doesn't that look lovely? If I put more effort in, I could clean it up. However, I use Redgate SQL Prompt to format all my code, including the example below.

SELECT CASE
           WHEN 1 = 1 THEN
               CASE
                   WHEN 2 = 2 THEN
                       CASE
                           WHEN 3 = 3 THEN
                               3
                           ELSE
                               0
                       END
                   ELSE
                       0
               END
           ELSE
               0
       END AS [My Second Favorite Number];

There are a few other places where you see CASE expressions; the first is in a WHERE clause:

SELECT Column3
FROM dbo.SpecialTable
WHERE CASE
          WHEN Column3 = 'Hello' THEN
              1
          WHEN Column3 = 'Goodbye' THEN
              2
          ELSE
              0
      END > 0;

And finally, in the example below, I've added it to the JOIN clause:

SELECT t1.Column1,
       t1.Column2,
       t2.Column3
FROM Table1 t1
    INNER JOIN SpecialTable t2
        ON CASE
               WHEN t2.Column3 = 'Hello' THEN
                   1
               WHEN t2.Column3 = 'Goodbye' THEN
                   2
               ELSE
                   0
           END = t1.JoinColumn;

As I mentioned above, people might frown on the second more than the first. I've seen it written on several "you should never do" lists.

Build Our Dataset

Let's create a dataset where using a CASE expression in a JOIN makes sense. I'll add two tables: Person and Sales.

CREATE TABLE dbo.Person
(
    Id INT NOT NULL,
    FirstName NVARCHAR(200) NOT NULL,
    LastName NVARCHAR(200) NOT NULL,
     IsActive BIT NOT NULL
    CONSTRAINT PK_Person_Id
        PRIMARY KEY CLUSTERED (Id)
);
CREATE TABLE dbo.Sales
(
    Id INT IDENTITY(1, 1) NOT NULL,
    SalesDate DATE NOT NULL,
    OrderCode NVARCHAR(3) NOT NULL,
    Quantity INT NOT NULL,
    Price DECIMAL(6, 0) NOT NULL,
    SalesPersonId INT NOT NULL,
    AccountManagerId INT NULL,
    CONSTRAINT PK_Sales_Id
        PRIMARY KEY CLUSTERED (Id),
    CONSTRAINT FK_Sales_SalesPerson
        FOREIGN KEY (SalesPersonId)
        REFERENCES dbo.Person (Id),
    CONSTRAINT FK_Sales_AccountManager
        FOREIGN KEY (AccountManagerId)
        REFERENCES dbo.Person (Id)
);
GO

Now, let's add some data to each of them. For the Person table, we'll insert 20 rows. For the Sales table, we'll insert 100,000 rows. I want at least one table to be on the larger side.

INSERT INTO dbo.Person
(
    Id,
    FirstName,
    LastName,
    IsActive
)
VALUES
(1, 'John', 'Smith', 1),
(2, 'Sarah', 'Johnson', 1),
(3, 'Michael', 'Williams', 1),
(4, 'Emily', 'Brown', 1),
(5, 'David', 'Jones', 1),
(6, 'Emma', 'Garcia', 1),
(7, 'Daniel', 'Martinez', 1),
(8, 'Olivia', 'Davis', 1),
(9, 'Matthew', 'Rodriguez', 1),
(10, 'Ava', 'Miller', 1),
(11, 'Christopher', 'Gonzalez', 1),
(12, 'Sophia', 'Wilson', 1),
(13, 'Andrew', 'Anderson', 1),
(14, 'Isabella', 'Thomas', 1),
(15, 'Joshua', 'Jackson', 1),
(16, 'Mia', 'White', 1),
(17, 'William', 'Harris', 1),
(18, 'Charlotte', 'Clark', 1),
(19, 'Ethan', 'Lewis', 1),
(20, 'Amelia', 'Lee', 1);



WITH SalesData AS (
    SELECT TOP 100000
        DATEADD(DAY, RAND(CHECKSUM(NEWID())) * (1 + DATEDIFF(DAY, '2023-04-01', '04-30-2023')), '2023-04-01') AS SalesDate,
        CASE
            WHEN (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) % 3 = 0 THEN 'ABC'
            WHEN (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) % 3 = 1 THEN 'DEF'
            ELSE 'GHI'
        END AS OrderCode,
        ABS(CHECKSUM(NEWID()) % 100) + 1 AS Quantity,
        ABS(CHECKSUM(NEWID()) % 1000) + 100 AS Price,
        ABS(CHECKSUM(NEWID()) % 10) + 1 AS SalesPersonId,
        CASE
            WHEN (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) % 100 = 0 THEN NULL
            ELSE ABS(CHECKSUM(NEWID()) % 10) + 11
        END AS AccountManagerId
      FROM sys.all_columns cl1
    CROSS JOIN sys.all_columns cl2
)
-- Insert the data into the dbo.Sales table
INSERT INTO dbo.Sales
(
    SalesDate,
    OrderCode,
    Quantity,
    Price,
    SalesPersonId,
    AccountManagerId
)
SELECT SalesDate,
       OrderCode,
       Quantity,
       Price,
       SalesPersonId,
       AccountManagerId
FROM SalesData;

We always populate the SalesPersonId in the Sales table. The AccountManagerId column is sometimes populated. You can see that the SalesPersonId and AccountManagerId are foreign keys back to the Person table.

Why Use a CASE Expression in a JOIN?

Why would you use a CASE in a JOIN clause in the first place? One reason is if you have some complex join logic you're trying to implement, essentially joining a table based on multiple conditions. For example, we want to join our Person and Sales tables primarily on the SalesPersonId. However, when the OrderCode has a specific value, we want to join on the AccountManagerId.

SELECT CONCAT(   p.LastName,
                 ', ',
                 p.FirstName
             ) AS PersonName,
       SUM(s.Price) AS TotalSales,
       s.SalesDate
FROM dbo.Sales s
    INNER JOIN dbo.Person p
        ON p.Id = CASE
                      WHEN s.OrderCode = 'ABC'
                           AND s.AccountManagerId IS NOT NULL THEN
                          s.AccountManagerId
                      ELSE
                          s.SalesPersonId
                  END
GROUP BY s.SalesDate,
         p.FirstName,
         p.LastName;

Another reason for using CASE is to handle NULLs returned from one of the tables. Of course, this is when an ISNULL() doesn't provide the results you're looking for.

SELECT CONCAT(   p.LastName,
                 ', ',
                 p.FirstName
             ) AS PersonName,
       SUM(s.Price) AS TotalSales,
       s.SalesDate
FROM dbo.Sales s
    INNER JOIN dbo.Person p
        ON p.Id = CASE
                      WHEN s.AccountManagerId IS NULL THEN
                          1
                      WHEN s.AccountManagerId IS NULL
                           AND s.SalesDate > '2023-04-05' THEN
                          2
                      ELSE
                          s.SalesPersonId
                  END
GROUP BY s.SalesDate,
         p.FirstName,
         p.LastName;

I'm sure there are other examples, but these two seem to come up the most.

Alternatives to CASE

We have yet to look at the performance of CASE versus another method. However, how can we accomplish the same results differently by not using a CASE expression? The one I see recommended the most is using a UNION ALL operator. That's the one we'll compare against.

Below is how you would write a statement to avoid using the CASE. The UNION ALL creates two datasets that match the join criteria and then combines them.

SELECT CONCAT(   p.LastName,
                 ', ',
                 p.FirstName
             ) AS PersonName,
       SUM(s1.Price) AS TotalSales,
       s1.SalesDate
FROM dbo.Sales s1
    INNER JOIN dbo.Person p
        ON p.Id = s1.SalesPersonId
WHERE s1.OrderCode <> 'ABC'
      OR s1.AccountManagerId IS NULL
GROUP BY s1.SalesDate,
         p.FirstName,
         p.LastName
UNION ALL
SELECT CONCAT(   p.LastName,
                 ', ',
                 p.FirstName
             ) AS PersonName,
       SUM(s2.Price) AS TotalSales,
       s2.SalesDate
FROM dbo.Sales s2
    INNER JOIN dbo.Person p
        ON p.Id = s2.AccountManagerId
WHERE s2.OrderCode = 'ABC'
      AND s2.AccountManagerId IS NOT NULL
GROUP BY s2.SalesDate,
         p.FirstName,
         p.LastName;

One thing to remember is that the above method goes against the DRY philosophy. Not that I advocate following the philosophy to a T.

Comparing Performance

Now it's time to compare both methods and see if one performs better. Before we do that, I'm going to create three indexes. The first is mainly for the statement with the CASE expression, and the other two are for the UNION ALL.

CREATE NONCLUSTERED INDEX IX_SalesPerson_AccountManager_OrderCode
ON dbo.Sales (
                 SalesPersonId,
                 AccountManagerId,
                 OrderCode
             )
INCLUDE (
            Price,
            SalesDate
        );


CREATE NONCLUSTERED INDEX IX_AccountManager_OrderCode
ON dbo.Sales (
                 AccountManagerId,
                 OrderCode
             )
INCLUDE (
            Price,
            SalesDate
        );

CREATE NONCLUSTERED INDEX IX_SalesPerson_OrderCode
ON dbo.Sales (
                 SalesPersonId,
                 OrderCode
             )
INCLUDE (
            Price,
            SalesDate
        );
GO

First, turn STATISTICS IO on and enable the actual execution plan. Then execute both statements in the same batch and see what the performance markers say.

SET STATISTICS IO ON;

SELECT        CONCAT(   p.LastName,
                 ', ',
                 p.FirstName
             ) AS PersonName,
       SUM(s.Price) AS TotalSales,
       s.SalesDate
FROM dbo.Sales s
    INNER JOIN dbo.Person p
        ON p.Id = CASE
                      WHEN s.OrderCode = 'ABC'
                           AND s.AccountManagerId IS NOT NULL THEN
                          s.AccountManagerId
                      ELSE
                          s.SalesPersonId
                  END
GROUP BY s.SalesDate,
         p.FirstName,
           p.LastName;

/* Second method using a UNION ALL operator*/
SELECT CONCAT(   p.LastName,                 ', ',
                 p.FirstName
             ) AS PersonName,
       SUM(s1.Price) AS TotalSales,
       s1.SalesDate
FROM dbo.Sales s1
    INNER JOIN dbo.Person p
        ON p.Id = s1.SalesPersonId
WHERE s1.OrderCode <> 'ABC'
      OR s1.AccountManagerId IS NULL
GROUP BY s1.SalesDate,
         p.FirstName,
         p.LastName
UNION ALL
SELECT CONCAT(   p.LastName,
                 ', ',
                 p.FirstName
             ) AS PersonName,
       SUM(s2.Price) AS TotalSales,
       s2.SalesDate
FROM dbo.Sales s2
    INNER JOIN dbo.Person p
        ON p.Id = s2.AccountManagerId
WHERE s2.OrderCode = 'ABC'
      AND s2.AccountManagerId IS NOT NULL
GROUP BY s2.SalesDate,
         p.FirstName,
           p.LastName;

SET STATISTICS IO OFF;
GO
Statistics IO results. Page Reads.
Execution Plan from both queries in one batch.

You can see from the images above that the UNION ALL makes up more than 50% of the batch, along with additional page reads on both tables. Ultimately, the difference isn't going to be life-changing. However, if someone says using a CASE expression in a JOIN is a lousy practice, ask them why. I don't advocate using a CASE expression in the join for every query. But there are instances where it comes in handy.

I'm curious. Do you ever use a CASE expression in a JOIN clause? Please let me know in the comments below.

Key Points

  • You can use a CASE expression in almost any part of a SQL statement, including the WHERE and JOIN.
  • Given the example, the CASE expression performed better in this tutorial than the UNION ALL. However, this isn't an absolute rule.
  • If someone says adding a CASE expression to a JOIN clause is a bad practice, ask them to explain why.

Union Budget 2024-25 – All You Need to Know

  Union Budget 2024-25 – All You Need to Know The Union Budget is a yearly financial plan presented by the Finance Minister of India for the...