PDA

View Full Version : Resolved Do Queries speed depend on size of table or on the number of rows to be selected


nanoop13
08-04-2009, 08:25 PM
Hello friends,Sorry for again bothering you guys as I am very much new to this sql thing.I was thinking that the size of rows in my table is around 50,000 and I wanted to select a few records from them say only 200, will my query take a long time to execute.My website is hosted on shared server.
Does the time to execute a query depends on the number of rows present in the table(50000) or it depends on the number of records to be selected(200)?
Please someone out there clarify my doubt because accordingly I will frame the table structure.
Thanks.

Old Pedant
08-04-2009, 10:05 PM
They depend on both. Generally, the number of rows in the table will have a bigger impact than the number selected, but as with most programing questions the actual answer is "It depends..."

*IF* your query is based on an INDEXED field (or fields) in the table, then the number of rows *usually* won't make that big a difference. But if your query means the DB has to search through *all* the actual rows, then the number of rows could matter a lot.

As for the number of records returned: Understand that when you make a query of a DB server, the DB server is running in a *separate process* (and maybe even on another machine...especially in a shared environment) and must send the records back to the web server across the "wire" that connects the two servers. (If both are on the same computer, the "wire" might just be a chunk of shared memory, but ...) So it's more the total amount of *data* than simply the number of records. 200 records, where each record is only (say) 100 bytes is only 20,000 bytes to send. 200 records where each record is (say) 1 megabyte is 200MB to send. A *lot* of difference!!!

So always try to SELECT *only* the fields you will actually use; try to never do "SELECT *" and thus wastefully transfer useless data.

And try to base your WHERE clause on indexed fields.

nanoop13
08-05-2009, 05:42 PM
Thanks a lot sir....your reply was really helpful and I am really glad you took out so much time for this post...Keep up the good work.