February 18, 2016 at 7:59 am
We purchase the software for our company and we cannot just update the database and create index.
February 18, 2016 at 8:05 am
Fixing this will require either changing the query or changing the index. Can you do either?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 18, 2016 at 9:03 am
Loner (2/18/2016)
We purchase the software for our company and we cannot just update the database and create index.
I'll ask again...
Jeff Moden (2/17/2016)
Loner (2/17/2016)
Ok, I give up, someone please help! We have a query that runs just fine with hard coded values but once we swap them out for parameters the query runs with no end in sight.This version consistently returns 8,429 rows in less than 2 seconds:
SELECT username, firstname, lastname
FROM Users
WHERE UpdateDt BETWEEN '2/11/2016' AND '2/12/2016';
This version will run well over an hours and not return data:
DECLARE @Dt1 DATETIME, @dt2 DATETIME;
SET @Dt1 = '2/11/2016';
SET @dt2 = '2/12/2016';
SELECT username, firstname, lastname
FROM Users
WHERE UpdateDt BETWEEN @Dt1 AND @dt2;
The UpdateDt column in the Users table is DATETIME so I wouldn't expect any datatype conversion issues.
Any theories on what could be causing this?
So what's the CREATE TABLE for this table look like, what are the indexes on it, and how many rows does it have?
--Jeff Moden
Change is inevitable... Change for the better is not.
February 18, 2016 at 9:17 am
Sergiy (2/17/2016)
Alan.B (2/17/2016)
Sergiy (2/17/2016)
Make the PK on UserID non-clustered and create a clustered index on UpdateDt.Unless UpdateDt is not unique. Then perhaps a clustered index on composite key consisting of UpdateDt and UserID (in that order).
Clustered index does not have to be unique.
Which does not mean I would object your suggestion.
As I'm sure you know, the clustered index actually does have to be unique. If the user-specified columns are not unique in themselves, SQL Server will add a Uniquifier to make them so.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
February 18, 2016 at 9:28 am
Loner...
Using SSMS, expand the table list, find the Users table, right click on it and chose Script table as > CREATE to Query window. Then copy the table script and post it for us. That will help us help you. Then run this and provide us the results:
USE <yourdbname>
GO
SELECT COUNT(*)
FROM users
GO
That's what you've been asked for....and gives everyone a good idea of what you are working with.
-SQLBill
February 18, 2016 at 2:32 pm
The table has 2357880092 number of records.
February 18, 2016 at 4:10 pm
Loner (2/18/2016)
The table has 2357880092 number of records.
Thanks. Please provide a CREATE TABLE statement including all indexes and constraints.
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
February 18, 2016 at 6:48 pm
Phil Parkin (2/18/2016)
As I'm sure you know, the clustered index actually does have to be unique. If the user-specified columns are not unique in themselves, SQL Server will add a Uniquifier to make them so.
Yes, I know. I quoted that article from MSDN on this forum last week.
🙂
But it has nothing to do with CREATE INDEX statement.
We can define any index as clustered.
SQL Server will fill up the gaps in its uniqueness behind the scenes, where needed.
_____________
Code for TallyGenerator
February 18, 2016 at 8:35 pm
Loner (2/18/2016)
The table has 2357880092 number of records.
I still need the CREATE TABLE statement and the indexes for the table so that I can build a large test table to test for a solution for you.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 19, 2016 at 3:01 am
Loner: It's a long topic already so I may have overlooked something. Did you already try the OPTION (RECOMPILE) hint that Scott suggested? Did it help?
February 19, 2016 at 7:06 am
Yes I put in Option recompile and still ran a long time.
February 19, 2016 at 7:09 am
I just look at the table, it has 12 FK and 1 PK and 11 non unique non cluster index.
February 19, 2016 at 7:10 am
Can you post execution plans of the fast and the slow versions please? Actual plans if possible.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 19, 2016 at 7:14 am
Loner (2/19/2016)
I just look at the table, it has 12 FK and 1 PK and 11 non unique non cluster index.
Why are you refusing to provide the information which the people here are requesting? There are some extremely proficient people contributing here, yet you are asking them to work blindfolded in a darkened room.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
February 19, 2016 at 8:01 am
Loner (2/19/2016)
I just look at the table, it has 12 FK and 1 PK and 11 non unique non cluster index.
I don't need the FK's. Just the CREATE TABLE and the indexes. If you're scripting options are setup correctly, the indexes will be generated at the same time you script out the table.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 16 through 30 (of 58 total)
You must be logged in to reply to this topic. Login to reply