How to improve the performance of my query which uses select top 20 and a bunch of isnull in the where clause?

user3442679
select top 20 id
from Employee e
where e.state=IsNull(@State,e.state)
and   e.industry=IsNull(@Industry,e.Industry)
Rahul

Two issue I see here is use if ISNULL() function in WHERE clause and using parameter in WHERE condition which makes it difficult to determine whether to use index seek or not.

Per my observation, you can either change your WHERE condition to be like

where e.state is null or e.state = @State
and   e.industry is null or e.Industry = @Industry

(OR) try using a dynamic query instead like

declare @sql varchar(200);
declare @cond varchar(100);

set @sql = 'select top 20 id from Employee e ';
if(@State is not null)
set @cond = @cond + ' and e.state = @State'
if(@Industry is not null)
set @cond = @cond + ' and e.industry = @Industry'

IF len(@cond) > 0
SET @sql = @sql + ' WHERE ' + RIGHT(@cond, LEN(@cond)-3)

Dynamic query idea taken from Here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to improve the performance of a SQL Server Select query?

From Dev

How to improve the performance of my query which uses select top 20 and a bunch of isnull in the where clause?

From Dev

How to improve performance of query execution

From Dev

How to improve my query with good performance to split a string with SQL Server

From Dev

select query in hibernate with where clause

From Dev

Moodle Reading data from Database: How to SELECT the first record which meets the WHERE clause? TOP does not work

From Dev

Improve my psql query performance

From Dev

How to improve sqlite SELECT performance?

From Dev

How to improve performance in cypher query with `OR`?

From Dev

How to improve performance of mysql query?

From Dev

How to improve performance of a simple select query in oracle

From Dev

IsNull in sybase where clause

From Dev

MySQL select query in where clause

From Dev

How to improve the performance of a SQL Server Select query?

From Dev

How to improve performance of query execution

From Dev

How to improve my query with good performance to split a string with SQL Server

From Dev

MySQL Optimizing a subquery which uses a WHERE clause?

From Dev

Select In query how to order in where clause racle

From Dev

Moodle Reading data from Database: How to SELECT the first record which meets the WHERE clause? TOP does not work

From Dev

Improve my psql query performance

From Dev

Rewrite query without where clause to improve performance

From Dev

How to improve sqlite SELECT performance?

From Dev

How to improve performance of my laptop?

From Dev

How to improve performance of mysql query?

From Dev

SQL Server: How to improve performance for queries with multiple CTEs and subqueries in the WHERE clause

From Dev

How to improve performance of this sql query?

From Dev

How to add a where clause to an EF query which has a grouping

From Dev

To optimize my linq query which listed as top consuming query in azure query performance insight tool

From Dev

How to improve performance of this query

Related Related

  1. 1

    How to improve the performance of a SQL Server Select query?

  2. 2

    How to improve the performance of my query which uses select top 20 and a bunch of isnull in the where clause?

  3. 3

    How to improve performance of query execution

  4. 4

    How to improve my query with good performance to split a string with SQL Server

  5. 5

    select query in hibernate with where clause

  6. 6

    Moodle Reading data from Database: How to SELECT the first record which meets the WHERE clause? TOP does not work

  7. 7

    Improve my psql query performance

  8. 8

    How to improve sqlite SELECT performance?

  9. 9

    How to improve performance in cypher query with `OR`?

  10. 10

    How to improve performance of mysql query?

  11. 11

    How to improve performance of a simple select query in oracle

  12. 12

    IsNull in sybase where clause

  13. 13

    MySQL select query in where clause

  14. 14

    How to improve the performance of a SQL Server Select query?

  15. 15

    How to improve performance of query execution

  16. 16

    How to improve my query with good performance to split a string with SQL Server

  17. 17

    MySQL Optimizing a subquery which uses a WHERE clause?

  18. 18

    Select In query how to order in where clause racle

  19. 19

    Moodle Reading data from Database: How to SELECT the first record which meets the WHERE clause? TOP does not work

  20. 20

    Improve my psql query performance

  21. 21

    Rewrite query without where clause to improve performance

  22. 22

    How to improve sqlite SELECT performance?

  23. 23

    How to improve performance of my laptop?

  24. 24

    How to improve performance of mysql query?

  25. 25

    SQL Server: How to improve performance for queries with multiple CTEs and subqueries in the WHERE clause

  26. 26

    How to improve performance of this sql query?

  27. 27

    How to add a where clause to an EF query which has a grouping

  28. 28

    To optimize my linq query which listed as top consuming query in azure query performance insight tool

  29. 29

    How to improve performance of this query

HotTag

Archive