How to improve execution speed on a select operation

Go To StackoverFlow.com

0

create table foo (id int(11) auto_increment, value varchar(255))

The table contains roughly 10,00,000 rows and select operations like select id, value from foo take around 2-3 secs to execute, how do I improve the speed here

2012-04-05 17:56
by Sam
"select id, value from foo" ... with no where clause - Alex Howansky 2012-04-05 18:02
no where claus - Sam 2012-04-05 18:08
"Well, there's your problem!" - Adam Savag - Alex Howansky 2012-04-05 18:10
pretty good response for a million rows to be returned.. i'd take that any da - Randy 2012-04-05 18:19


2

select id, value from foo is going to return the whole table. The 2-3 seconds you mention could include the time it takes to transfer your results. This can often be much longer than it takes you database server to execute the actual query.

2012-04-05 18:04
by Dave Lowerre


1

If there is no WHERE clause (like the query you posted) and you are selecting one million rows, there is a good chance that rather than the query taking 2-3 secs to execute it's in fact taking that time to send the data across the network.

Can your application actually use a million database records at once? If you are showing that data to a user then you are likely going to be paging it, so change your query to select data in more manageable chunks.

SELECT id, value 
FROM foo
LIMIT $offset, $pgsize -- these two values will come from your application

If you really, really need all those rows at once, then I suggest you cache them.

2012-04-05 18:16
by mafue


0

Add an index to the fields used in the where clause.

2012-04-05 17:58
by Jens Schauder


0

CREATE INDEX id_index ON foo (id) USING BTREE;

You can see more info at http://dev.mysql.com/doc/refman/5.0/en/create-index.html

2012-04-05 18:03
by Fermin Silva
Ads