SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Alternatives To Left Join: Poor Performance of Procedure


Alternatives To Left Join: Poor Performance of Procedure

Author
Message
chandan_jha18
chandan_jha18
SSC Eights!
SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)

Group: General Forum Members
Points: 897 Visits: 2134
Hi,

I have a question here for a query where the tables involved are not too big but I suspect that the left join in the query is slowing things little. I have 2 tables involved here: Player and PartnerPlayer. The PartnerPlayer has a field to be returned and there is one-on-one relation between these two tables on a column called PlayerId.

So basically I am trying to get the data from Player table and 1 column from PartnerPlayer table. For the players who have entry in PartnerPlayer, an account number is displayed else NULL is returned for those rows from Player table who does not exist in the PartnerPlayer.

Please let me know if the query can rewritten given that each of the tables involved have around 100k records.

Thanks
Chandan Jha
Attachments
Table_Schema.txt (26 views, 2.00 KB)
Plan.sqlplan (25 views, 23.00 KB)
Procedure_Schema.txt (30 views, 3.00 KB)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88505 Visits: 45284
Sounds like a NOT EXISTS, but it's not going to be measurable difference in performance just from changing that.

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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5940 Visits: 5084
Hi
You have an existing index

CREATE NONCLUSTERED INDEX [IX_Player_partnerIddtCreated] ON [dbo].[Player]
(
[partnerId] ASC,
[dtCreated] ASC
)
INCLUDE ( [playerId])

If you take a look in your execution plan you'll see a missing index. You can create it, but it's wiser if you could extend the already existing one [IX_Player_partnerIddtCreated] with the following definition:

CREATE NONCLUSTERED INDEX [IX_Player_partnerIddtCreated]
ON [dbo].[Player] ([partnerId] ASC,
[dtCreated] ASC)
INCLUDE ([playerId],[partnerId],[email],[alias],[forename],[surname],[firstGame],[firstMoneyGame])

Tables are not big so you have a performance issue.

Regards,
IgorMi

Igor Micev,
SQL Server developer at Seavus
My blog: www.igormicev.com
Grant Fritchey
Grant Fritchey
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40625 Visits: 32666
And ditch the NOLOCK hint.

If the table has 100K of records, this looks like it's also returning over 100K of records. Is the WHERE clause not filtering at all?

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
chandan_jha18
chandan_jha18
SSC Eights!
SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)SSC Eights! (897 reputation)

Group: General Forum Members
Points: 897 Visits: 2134
Grant Fritchey (12/17/2013)
And ditch the NOLOCK hint.

If the table has 100K of records, this looks like it's also returning over 100K of records. Is the WHERE clause not filtering at all?


Sir, this application has nolock hints everywhere because this is a very very heavy gaming database and this is what the management wants to stick to.

Also, you are seeing so many records returned because the execution plan that I sent you is from a query for the partner which had most number of players.

I think a lot of such joins occur because of bad schema during the initial phase and not many predict how the data is going to behave 1 year later. also, in this case any indexing is not helping.

Thanks
Chandan
twin.devil
twin.devil
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2399 Visits: 2675
Try to execute the procedure without using 'dbo.ConvertTimeZoneFromUTC'. Table 'PartnerPlayer' is missing an index. (Missing a Primary key, there Should be). You can also use NOT EXISTS instead of Left Outer Join.
Grant Fritchey
Grant Fritchey
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40625 Visits: 32666
chandan_jha18 (12/17/2013)
Grant Fritchey (12/17/2013)
And ditch the NOLOCK hint.

If the table has 100K of records, this looks like it's also returning over 100K of records. Is the WHERE clause not filtering at all?


Sir, this application has nolock hints everywhere because this is a very very heavy gaming database and this is what the management wants to stick to.

Also, you are seeing so many records returned because the execution plan that I sent you is from a query for the partner which had most number of players.

I think a lot of such joins occur because of bad schema during the initial phase and not many predict how the data is going to behave 1 year later. also, in this case any indexing is not helping.

Thanks
Chandan


And management is fully aware that NOLOCK means that they can return bad data, inaccurate data, missing rows, extra rows, and they're OK with that? It's pretty rare the business that doesn't care if the same query run twice returns completely different results without data changes.

The thing is, you're only joining a couple of tables. That's pretty normal for a relational database. In fact, joining together 6,10 even 20 tables isn't completely insane on a relational system. The issue is, you're looking at table scans. No matter how you do your joins, table scans are likely to lead to slow performance. So you need to determine how to reduce those scans AND get good performance on your joins and your order by statements. Further, it looks like PartnerPlayer is a heap table. That's likely going to cause a number of issues too.

But, I'm still back to the amount of data. The table cardinality is 2.7K and this query is returning 1/2 of it. You're probably only ever going to get scans on that level of data retrieval. I get that this is an outlier in terms of the data, but simply retrieving it the same way as other data is going to lead to this situation, too much data going through for it to perform well. You need to filter more. Unless this is an export, no one consumes 100K rows of data. So they're likely doing something like looking at the top 100 or sorting by region or something, build that into the query so that you're filtering better and then you can apply meaningful indexes that help performance. As long as you're returning half the table, you're stuck.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)

Group: General Forum Members
Points: 7997 Visits: 7161
Rewriting the query and/or adding nonclustered indexes won't help, unless you basically rewrite the entire table by including a gazillion in the nonclus index(es) -- and constantly maintain it as another column gets referenced in a query.

The way to really solve these types of query issues is to first get the best clustered indexes.

If you (almost) always specify a date range when querying dbo.Player, as in the sample queries/code you posted, change the indexes as scripted below.

Naturally this may take quite a while, as the tables will have to be written/rewritten during this process. Of course remove the "ONLINE = ON," if that doesn't work on your system.



ALTER TABLE dbo.PartnerPlayer DROP CONSTRAINT fk_PartnerPlayer_partnerIdPlayerId;

DROP INDEX IX_Player_partnerIddtCreated ON dbo.Player;

ALTER TABLE dbo.Player DROP CONSTRAINT PK_Player;

CREATE CLUSTERED INDEX CL_Player ON dbo.Player ( dtCreated ) WITH ( FILLFACTOR = 99, SORT_IN_TEMPDB = ON ) ON [PRIMARY];

CREATE UNIQUE CLUSTERED INDEX CL_Player ON dbo.Player ( dtCreated, partnerId, playerId ) WITH ( FILLFACTOR = 99, SORT_IN_TEMPDB = ON ) ON [PRIMARY];

CREATE UNIQUE NONCLUSTERED INDEX IX_Player_partnerId_playerId ON dbo.Player ( partnerId, playerId ) WITH ( FILLFACTOR = 96, ONLINE = ON, SORT_IN_TEMPDB = ON ) ON [PRIMARY];

CREATE CLUSTERED INDEX CL_PartnerPlayer ON dbo.PartnerPlayer ( partnerId, playerId ) WITH ( FILLFACTOR = 96, SORT_IN_TEMPDB = ON ) ON [PRIMARY];

ALTER TABLE [dbo].[PartnerPlayer] WITH CHECK
ADD CONSTRAINT [fk_PartnerPlayer_partnerIdPlayerId] FOREIGN KEY([partnerId], [playerId])
REFERENCES [dbo].[Player] ([partnerId], [playerId]);
ALTER TABLE [dbo].[PartnerPlayer] CHECK CONSTRAINT [fk_PartnerPlayer_partnerIdPlayerId];




Edit: Changed index definition.

SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search