HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Querying a warehouse database

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
warehousedatabasequerying

Problem

Consider the query given below:

```
SELECT * FROM ((SELECT c.cust_id ,
c.username ,
REPLACE(qs.seg_type_ref_key_02, 'N/A', 'Non Vip') casino_group,
REPLACE(qs.seg_type_ref_key_03, 'N/A', 'Non Vip') bingo_group,
REPLACE(qs.seg_type_ref_key_04, 'N/A', 'Non Vip') games_group,
REPLACE(qs.seg_type_ref_key_12, 'N/A', 'Non Vip') poker_group,
REPLACE(qs.seg_type_ref_key_01, 'N/A', 'Non ViP') sportsbook_group,
c.country
, c.contactable
, c.email
, c.dob
, c.[status]
, c.first_name
, c.last_name
, c.[master]
, c.[language]
, c.gender
FROM warehouse.dbo.dim_customer c (nolock)

INNER JOIN warehouse . dbo . dim_segmentationcodehistory ( nolock )sc
ON sc . cust_id = c . cust_id
INNER JOIN warehouse . dbo . q_dim_segments qs ( nolock )
ON sc . seg_code_ref_key = qs . seg_code_ref_key
WHERE SC.active=1 and qs.seg_type_ref_key_04 <>'N/A' AND c.active = 1
and (qs.seg_type_ref_key_02 <> 'Prospect' and qs.seg_type_ref_key_03 <> 'Prospect' and qs.seg_type_ref_key_04 <> 'Prospect' and qs.seg_type_ref_key_12 <> 'Prospect' and qs.seg_type_ref_key_01 <> 'Prospect')) A
LEFT JOIN( SELECT c.cust_id cust_dup,
SUM(fc.turnover) AS Turnover_GBP,
SUM(fc.grosswin) AS GrossWin_GBP,
SUM(fc.chip_purch_amount_gbp) AS chip_purch_amount_gbp
FROM warehouse.dbo.fact_games fc (nolock)
INNER JOIN warehouse.dbo.dim_date d (nolock)
ON d.date_key = fc.date_ref_key
INNER JOIN warehouse.dbo.dim_customer c (nolock)
ON c.cust_ref_key = fc.cust_ref_key
INNER JOIN warehouse.dbo.dim_gamesgame gg(nolock)
ON gg.games_game_ref_key = fc.game_ref_key
WHERE d.[date] between getdate()- 10 AND getdate()-9
AND gg.Game_Group_Description <> 'Bingo'
GROUP BY c.cust_id )B
ON A.cust_id = B.

Solution

From some research, I have found the <> or does not equal takes longer to run than = operations

In this answer to SQL Server “<>” operator is very slow compared to “=” on table with a few million rows the Poster goes into Detail about the <> operator and why it takes longer than the = operator.

The Poster suggests a LEFT JOIN for the OP's Query that would make it so that they shouldn't have to use the <> operator.

If you could do something similar to weed out the records that you don't want using a LEFT JOIN or something of the sort. I think the post that I linked to should set you in the right direction.

P.S.

It sounds like the same thing happens when you use NOT IN

Context

StackExchange Code Review Q#30745, answer score: 2

Revisions (0)

No revisions yet.