Struggling to create an efficient index for this query












0















I am struggling to create an index for a query and unfortunately I cannot change the query at all as it part of an ERP system. The problem is this query has over 1m reads and sometimes a duration of 10s



I Have tried using just the predicate, the seek predicate and a combination of both in different orders to no success.



Here is the query that cannot be changed:



SELECT  TOP (@0) "timestamp","Link ID","Record ID","URL1","URL2","URL3","URL4","Description","Type","Created","User ID","Company","Notify","To User ID" 
FROM "Database".dbo."Record Link" WITH(READUNCOMMITTED)
WHERE ("Link ID">@1 AND (("Company"=@2 OR "Company"=@3) AND "Notify"=@4 AND ("To User ID" COLLATE Latin1_General_100_CI_AI LIKE @5 OR "To User ID" COLLATE Latin1_General_100_CI_AI LIKE @6)))
ORDER BY "Link ID" ASC OPTION(OPTIMIZE for UNKNOWN, FAST 50)


Here is the script for the table if it helps:



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Record Link](
[timestamp] [timestamp] NOT NULL,
[Link ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Record ID] [varbinary](448) NOT NULL,
[URL1] [nvarchar](250) NOT NULL,
[URL2] [nvarchar](250) NOT NULL,
[URL3] [nvarchar](250) NOT NULL,
[URL4] [nvarchar](250) NOT NULL,
[Description] [nvarchar](250) NOT NULL,
[Type] [int] NOT NULL,
[Note] [image] NULL,
[Created] [datetime] NOT NULL,
[User ID] [nvarchar](132) NOT NULL,
[Company] [nvarchar](30) NOT NULL,
[Notify] [tinyint] NOT NULL,
[To User ID] [nvarchar](132) NOT NULL,
CONSTRAINT [Record Link$0] PRIMARY KEY CLUSTERED
(
[Link ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO


Here is the stats for the time and IO



  SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 4 ms.

(0 rows affected)
Table 'Record Link'. Scan count 1, logical reads 1018402, physical reads 3, read-ahead reads 1018391, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(1 row affected)

SQL Server Execution Times:
CPU time = 8234 ms, elapsed time = 12641 ms.


Here is the execution plan:
enter image description here



I would greatly appreciate it if someone could help me create an efficient index for this.









share







New contributor




BainAnator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Please paste the plan and add link to the question.

    – vonPryz
    1 min ago
















0















I am struggling to create an index for a query and unfortunately I cannot change the query at all as it part of an ERP system. The problem is this query has over 1m reads and sometimes a duration of 10s



I Have tried using just the predicate, the seek predicate and a combination of both in different orders to no success.



Here is the query that cannot be changed:



SELECT  TOP (@0) "timestamp","Link ID","Record ID","URL1","URL2","URL3","URL4","Description","Type","Created","User ID","Company","Notify","To User ID" 
FROM "Database".dbo."Record Link" WITH(READUNCOMMITTED)
WHERE ("Link ID">@1 AND (("Company"=@2 OR "Company"=@3) AND "Notify"=@4 AND ("To User ID" COLLATE Latin1_General_100_CI_AI LIKE @5 OR "To User ID" COLLATE Latin1_General_100_CI_AI LIKE @6)))
ORDER BY "Link ID" ASC OPTION(OPTIMIZE for UNKNOWN, FAST 50)


Here is the script for the table if it helps:



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Record Link](
[timestamp] [timestamp] NOT NULL,
[Link ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Record ID] [varbinary](448) NOT NULL,
[URL1] [nvarchar](250) NOT NULL,
[URL2] [nvarchar](250) NOT NULL,
[URL3] [nvarchar](250) NOT NULL,
[URL4] [nvarchar](250) NOT NULL,
[Description] [nvarchar](250) NOT NULL,
[Type] [int] NOT NULL,
[Note] [image] NULL,
[Created] [datetime] NOT NULL,
[User ID] [nvarchar](132) NOT NULL,
[Company] [nvarchar](30) NOT NULL,
[Notify] [tinyint] NOT NULL,
[To User ID] [nvarchar](132) NOT NULL,
CONSTRAINT [Record Link$0] PRIMARY KEY CLUSTERED
(
[Link ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO


Here is the stats for the time and IO



  SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 4 ms.

(0 rows affected)
Table 'Record Link'. Scan count 1, logical reads 1018402, physical reads 3, read-ahead reads 1018391, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(1 row affected)

SQL Server Execution Times:
CPU time = 8234 ms, elapsed time = 12641 ms.


Here is the execution plan:
enter image description here



I would greatly appreciate it if someone could help me create an efficient index for this.









share







New contributor




BainAnator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Please paste the plan and add link to the question.

    – vonPryz
    1 min ago














0












0








0








I am struggling to create an index for a query and unfortunately I cannot change the query at all as it part of an ERP system. The problem is this query has over 1m reads and sometimes a duration of 10s



I Have tried using just the predicate, the seek predicate and a combination of both in different orders to no success.



Here is the query that cannot be changed:



SELECT  TOP (@0) "timestamp","Link ID","Record ID","URL1","URL2","URL3","URL4","Description","Type","Created","User ID","Company","Notify","To User ID" 
FROM "Database".dbo."Record Link" WITH(READUNCOMMITTED)
WHERE ("Link ID">@1 AND (("Company"=@2 OR "Company"=@3) AND "Notify"=@4 AND ("To User ID" COLLATE Latin1_General_100_CI_AI LIKE @5 OR "To User ID" COLLATE Latin1_General_100_CI_AI LIKE @6)))
ORDER BY "Link ID" ASC OPTION(OPTIMIZE for UNKNOWN, FAST 50)


Here is the script for the table if it helps:



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Record Link](
[timestamp] [timestamp] NOT NULL,
[Link ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Record ID] [varbinary](448) NOT NULL,
[URL1] [nvarchar](250) NOT NULL,
[URL2] [nvarchar](250) NOT NULL,
[URL3] [nvarchar](250) NOT NULL,
[URL4] [nvarchar](250) NOT NULL,
[Description] [nvarchar](250) NOT NULL,
[Type] [int] NOT NULL,
[Note] [image] NULL,
[Created] [datetime] NOT NULL,
[User ID] [nvarchar](132) NOT NULL,
[Company] [nvarchar](30) NOT NULL,
[Notify] [tinyint] NOT NULL,
[To User ID] [nvarchar](132) NOT NULL,
CONSTRAINT [Record Link$0] PRIMARY KEY CLUSTERED
(
[Link ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO


Here is the stats for the time and IO



  SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 4 ms.

(0 rows affected)
Table 'Record Link'. Scan count 1, logical reads 1018402, physical reads 3, read-ahead reads 1018391, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(1 row affected)

SQL Server Execution Times:
CPU time = 8234 ms, elapsed time = 12641 ms.


Here is the execution plan:
enter image description here



I would greatly appreciate it if someone could help me create an efficient index for this.









share







New contributor




BainAnator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I am struggling to create an index for a query and unfortunately I cannot change the query at all as it part of an ERP system. The problem is this query has over 1m reads and sometimes a duration of 10s



I Have tried using just the predicate, the seek predicate and a combination of both in different orders to no success.



Here is the query that cannot be changed:



SELECT  TOP (@0) "timestamp","Link ID","Record ID","URL1","URL2","URL3","URL4","Description","Type","Created","User ID","Company","Notify","To User ID" 
FROM "Database".dbo."Record Link" WITH(READUNCOMMITTED)
WHERE ("Link ID">@1 AND (("Company"=@2 OR "Company"=@3) AND "Notify"=@4 AND ("To User ID" COLLATE Latin1_General_100_CI_AI LIKE @5 OR "To User ID" COLLATE Latin1_General_100_CI_AI LIKE @6)))
ORDER BY "Link ID" ASC OPTION(OPTIMIZE for UNKNOWN, FAST 50)


Here is the script for the table if it helps:



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Record Link](
[timestamp] [timestamp] NOT NULL,
[Link ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Record ID] [varbinary](448) NOT NULL,
[URL1] [nvarchar](250) NOT NULL,
[URL2] [nvarchar](250) NOT NULL,
[URL3] [nvarchar](250) NOT NULL,
[URL4] [nvarchar](250) NOT NULL,
[Description] [nvarchar](250) NOT NULL,
[Type] [int] NOT NULL,
[Note] [image] NULL,
[Created] [datetime] NOT NULL,
[User ID] [nvarchar](132) NOT NULL,
[Company] [nvarchar](30) NOT NULL,
[Notify] [tinyint] NOT NULL,
[To User ID] [nvarchar](132) NOT NULL,
CONSTRAINT [Record Link$0] PRIMARY KEY CLUSTERED
(
[Link ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO


Here is the stats for the time and IO



  SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 4 ms.

(0 rows affected)
Table 'Record Link'. Scan count 1, logical reads 1018402, physical reads 3, read-ahead reads 1018391, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(1 row affected)

SQL Server Execution Times:
CPU time = 8234 ms, elapsed time = 12641 ms.


Here is the execution plan:
enter image description here



I would greatly appreciate it if someone could help me create an efficient index for this.







sql-server index nonclustered-index





share







New contributor




BainAnator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share







New contributor




BainAnator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share



share






New contributor




BainAnator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 9 mins ago









BainAnatorBainAnator

82




82




New contributor




BainAnator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





BainAnator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






BainAnator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • Please paste the plan and add link to the question.

    – vonPryz
    1 min ago



















  • Please paste the plan and add link to the question.

    – vonPryz
    1 min ago

















Please paste the plan and add link to the question.

– vonPryz
1 min ago





Please paste the plan and add link to the question.

– vonPryz
1 min ago










0






active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






BainAnator is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f228990%2fstruggling-to-create-an-efficient-index-for-this-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








BainAnator is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















BainAnator is a new contributor. Be nice, and check out our Code of Conduct.













BainAnator is a new contributor. Be nice, and check out our Code of Conduct.












BainAnator is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Database Administrators Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f228990%2fstruggling-to-create-an-efficient-index-for-this-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liste der Baudenkmale in Friedland (Mecklenburg)

Single-Malt-Whisky

Czorneboh