SQL Server query speed is difference when filtering by different columns -
i have database 1 table logs
contains columns:
id
(pk clustered, int, not null),servicename
(nvarchar(255), not null) , other columnstaskvariant
(nvarchar(1024)),source
(nvarchar(1024)).
i have created index index_servicename
(non-unique, non-clustered) on servicename
column , includes columns except id, servicename
.
- database size around 4 gb.
- table contains around 3 500 000 rows.
- table contains around 1 400 000 rows source = n'ipjob'.
- table contains around 2 400 000 rows taskvariant = n'ip'.
- table contains around 600 000 rows servicename = '1' , taskvariant = n'ip'.
- table contains around 350 000 rows servicename = '1' , source = n'ipjob'.
problem:
i want select columns table filtering servicename
, taskvariant
or source
paging. original query selecting last 100 items filtering source
is:
select top (100) [filter1].[id] [id], [filter1].[date] [date], [filter1].[data] [data], [filter1].[servicename] [servicename], [filter1].[loglevel] [loglevel], [filter1].[stacktrace] [stacktrace], [filter1].[taskvariant] [taskvariant], [filter1].[source] [source], [filter1].[message] [message] ( select [extent1].[id] [id], [extent1].[date] [date], [extent1].[data] [data], [extent1].[servicename] [servicename], [extent1].[loglevel] [loglevel], [extent1].[stacktrace] [stacktrace], [extent1].[taskvariant] [taskvariant], [extent1].[source] [source], [extent1].[message] [message], row_number() on (order [extent1].[id] desc) [row_number] [dbo].[logs] [extent1] (@servicename = [extent1].[servicename]) , (@source = [extent1].[source]) ) [filter1] [filter1].[row_number] > 0 order [filter1].[id] desc
this query works fast ~ 00:00:00 time.
but when tried filter taskvariant
query takes ~ 00:02:18 minutes (next query).
select top (100) [filter1].[id] [id], [filter1].[date] [date], [filter1].[data] [data], [filter1].[servicename] [servicename], [filter1].[loglevel] [loglevel], [filter1].[stacktrace] [stacktrace], [filter1].[taskvariant] [taskvariant], [filter1].[source] [source], [filter1].[message] [message] ( select [extent1].[id] [id], [extent1].[date] [date], [extent1].[data] [data], [extent1].[servicename] [servicename], [extent1].[loglevel] [loglevel], [extent1].[stacktrace] [stacktrace], [extent1].[taskvariant] [taskvariant], [extent1].[source] [source], [extent1].[message] [message], row_number() on (order [extent1].[id] desc) [row_number] [dbo].[logs] [extent1] (@servicename = [extent1].[servicename]) , (@taskvariant = [extent1].[taskvariant]) ) [filter1] [filter1].[row_number] > 0 order [filter1].[id] desc
question: why second query execute slower , how solve issue?
thank suggestions.
execution plans1
the difference in execution time seeing due fact first has index , second doesn't. why it's such large difference, it's fact since there's index, means values sorted.
since values sorted, can use efficient string search algorithms can make number of operations when filtering orders of magnitude smaller.
in addition, there's lot of other characteristics can affect this. it's possible entire index in memory, while table data not, filtering in first query done on memory , never touch disk, other may not.
Comments
Post a Comment