0

I have a mysql memory table with contains priorities assign to each row.I am creating this table every 2 minutes and this table is being called almost 100 times/second.

It contains around 800 rows.

Query I am running is like:

select * from table order by priority limit 0,20 

I want to optimize this query.Can I put the data in the sorted order while creating the table and can assume data pulled will always be sorted based on priority if I remove the order by clause.

Thanks

shashuec
  • 684
  • 8
  • 20

2 Answers2

0

I have 2 suggestions:

  1. If you create this table and do NOT modify after creation it then after creating and filling do this ALTER TABLE table ORDER BY priority. This will sort your table and you can give up your ordering.
  2. Don't use mysql to order your results. Use php for it.

Also dont forget about indexes. If you didn't turn off mysql result caching then all your selects are cached and don't calculates results. So you have to check your config for it. Read more here

Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
0

There are 3 things you can do database-side & 1 on the actual HTML/PHP side to optimise this query:

  1. Use an ISAM table with 'priority' indexed @ http://dev.mysql.com/doc/refman/5.0/en/create-index.html & also make it as small as possible e.g. 'priority' SMALLINT(3) UNSIGNED ZEROFILL . The last point, limiting, you've already got.

  2. Use an opcode cacher to cache the results and deliver them directly to people's browsers if the information is only changing every-so-often, without requerying the database e.g. http://en.wikipedia.org/wiki/List_of_PHP_accelerators

Atari
  • 143
  • 2
  • 10