Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Another efficiency question for the experts

$
0
0

Hello SQL experts!

I have another question regarding SQL efficiency. This time for a query. Would you mind taking a look at the query below and giving your input on how it could be written to be more efficient? Currently it only takes 1 minute and 40 seconds to return the results but the sqlserver service CPU usage fluctuates between 25% and 40%. For what I want to do, this may just be "the way it is" but I thought asking SQL experts for their opinion/advice can only be a good thing.

The idea here is to get records from DB1.TB1 that haven't been modified in the last 30 days, have a value of 30 in the IntegerField AND where the BitField is false AND that don't have a message in DB2.TB2 within the last 30 days. DB1.TB1 has a single record for each IdentifierField. DB2.TB2 can have many records for each IdentifierField.

DB1.TB1 can have hundreds of thousands of records. DB2.TB2 can have millions of records. The performance described above is with DB1.TB1 having 110,000 records and DB2.TB2 having 37 million records.

Query: Select Database1.dbo.Table1.IdentifierField from Database1.dbo.Table1 where Database1.dbo.Table1.BitField=0 AND Database1.dbo.Table1.LastModifiedDate <= '10/9/2013 1:35:53 PM' AND Database1.dbo.Table1.IntegerField=30 except select IdentifierField from Database2.dbo.Table2 where MessageString='MessageA' AND MessageDateTime>='10/9/2013 1:35:53 PM'

I went with the "except" clause because "join" was taking much longer to return the results.

Thank you in advance!


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>