r/SQL • u/Konaseema • Sep 19 '22
MS SQL [MS SQL] SQL Server 2019: simple select * from 3.5 million rows table is taking around 1 minute 35 seconds. How to improve the performance?
Hello,
SQL Server 2019: simple select * from the table of 3.5 million rows table is taking around 1 minute 35 seconds. How to improve the performance?
Edit: Thank you everyone for your responses. I'm just testing how much time it will take just to do a simple select *. This table will be used by the majority of other tables in the warehouse. Trying to understand how best we can setup hardware/Index for better performance for future complex queries.
Any tips and techniques to change hardware or index info ?
14
Upvotes
10
u/planetmatt Sep 19 '22
What's the WHERE or JOIN clause?
Are there any existing indexes?
45
u/alinroc SQL Server DBA Sep 19 '22
Without doing any filtering (a
whereorjoinclause), you're schlepping 3.5M rows worth of data across the network and you're going to see a delay there just because of the volume. Especially if the table is wide.Why are you pulling this much data in the first place?
Run your query with
set statistics time onand check CPU time vs. elapsed time. If there's a wide gap between them, it's the network and/or your client application and has nothing at all to do with "performance."9
u/gandalf-duh-gray Sep 20 '22 edited Sep 20 '22
Number of rows is a useless metric. It can be as little as a few Megabytes or as big as a few Petabytes.
13
u/qwertydog123 Sep 20 '22
Don't use
SELECT *...Your query will almost certainly be constrained by network I/O, but if it's not there's a couple of things that will help:
ENABLE_PARALLEL_PLAN_PREFERENCEto force a parallel plan6
u/Joe59788 Sep 20 '22
Select top 5 *
7
u/gandi800 Sep 19 '22
If you're REALLY trying to return all 3.5 million rows (which I can't fathom a practical reason to do so) then you may be l in mited by the hardware and network. Especially if there are a lot of columns and even more so if there are blobs or text columns, I believe those are stored as side tables but could be wrong.
Upgrade your hardware and you might see performance gains, but in reality you should be using JOINs and your WHERE clause to filter down to what you actually need. That's just a lot of data and is doing to take time to retrieve off the drive and send over the network (if you're not logged into the server, which is something else you shouldn't do).
2
u/babygrenade Sep 20 '22
How wide is the table? Is the table fragmented?
2
u/Qkumbazoo Sep 20 '22
Were you waiting for all 3.5m rows to load?
How long did it take for the initial set of data to appear?
2
u/kelevenplusmistake Sep 20 '22
you need a query plan to see what the problem is. You can troubleshoot the query plan with the order of operations.
Have you done something that is causing it to run RBAR (Row by agonizing Row). SQL is about sets. How are you constructing them. I'm 99% sure you'll see in the plan where it blows up. It might be that the size of your data is too large. In that case it will require some other thoughts like hardware.
Read up about query plans here
2
u/Xmithiee Sep 20 '22
Never use * . Always list only columns that you actually need, if possible. That will narrow amount of data it needs to "transfer". Also filtering by index is a great idea, using where, joins, maybe the select iself would help us to advice you. However I feel like its normal, first of all the problem is in selecting ALL columns and selecting 3.5 million rows, thats what I would focus on.
Fun fact. At my job, we have a table with 4.5 billion rows, I once tried operation over the whole table ( on copied db ) and it didnt finish in a day, so yeah, narrow your select, if you really need to do that much stuff, dig into query plans, or do it by batches, can't really help since we dont know what you need the data for.
3
u/PossiblePreparation Sep 20 '22
Why do you care? I’m not sure what the world record for speed reading is but I don’t think there’s any human on the planet that will benefit from having data streamed to them that fast.
3
u/n_dev_00 Sep 19 '22
Index, partitions
1
u/[deleted] Sep 20 '22
I'd say adding TOP 100 to your select will make it return much faster. YMMV on data integrity. J/K!
0
u/Data_Engineering411 Sep 20 '22
Clickbait?