views:

37

answers:

3

I am planning to create a mysql 5 (myISAM) table that will contain x thousand rows of data. Each row will have a count field, & the rows with the 20 highest count values will be retrieved quite a lot, probably on a one-for-one ratio to every row update. The x thousand rows not in this 20 will not typically be retrieved.

What are the choices wrt choosing an efficient method of querying for the 20 top count values. Data does not have to be realtime live but it helps!

A: 

Every five minutes, empty out and then copy the top 20 to a separate table. To get the top 20, just consult that table. Done!

(Or, you could spend the next few hours tweaking your query and parameters and indices and making it all "just right".)

Steven Schlansker
Why the downvotes? I'd appreciate at least a comment as to why you don't like this idea...
Steven Schlansker
+1  A: 

Add an index to the count field, then just a normal query:

SELECT * FROM myTable ORDER BY countField DESC LIMIT 20;
nickf
A: 

Create an index on the count field. Done.

Byron Whitlock
fastest finger first. Just in case you wondered, I had thought about the polling solution, maybe even just copying the 20 busiest rows to a separate table and putting their updates on that, maybe 'reindexing' the 20 daily or whatever... but is an index always the correct solution in these scenarios?
rutherford
For a table with only a thousands of rows, an index will be just fine. when you get to millions of rows, cutting down the size of the dataset can be worth the extra effort.
Byron Whitlock