简体   繁体   中英

Can this mysql/myisam table be optimized?

I run a website where I need to store the information like this:

table: logs
 * date  (date)
 * server_1 (unsigned int)
 * server_2 (unsigned  int)
 * user_id (unsigned  int)
 * ip (unsigned int)
 * service (enum)
 * traffic (unsigned bigint)

My queries look primarliy like this:

SELECT SUM(traffic) FROM logs WHERE user_id = 8381 AND date > DATE_ADD(CURDATE(), INTERVAL -7 DAY) AND service != 'unknown'

I have a composite primary key over all the fields except traffic and an index on user_id and service

is it maby more efficient to store the traffic as a float?

Also is MyIsam a good engine or should I use innodb?

Or even another database system?

The table gets really quite big (tens of millions of rows) and is relly heavily queried.

MySQL won't use a composite index unless it uses the first "n" fields in the index:

MySQL can use multiple-column indexes for queries that test all the columns in the index, or queries that test just the first column, the first two columns, the first three columns, and so on. If you specify the columns in the right order in the index definition, a single composite index can speed up several kinds of queries on the same table.

At the very least create a separate index for each important field, especially the date column.

As Alnitak points out, the question is how your indexes are defined. One index is probably not enough, if you want fast access on these logs.

Optimization is usually done only by indexing (at least in your case). To answer your other questions:

  • casting the traffic attribute to a float won't bring performance, it shouldn't make a difference.
  • MyISAM is fast! InnoDB will be slower, because it has all its DB transaction overhead. But: It depends on what you want. MyISAM can be really dangerous, because it can break always (Poweroffs, etc). Speed would be the main reason to use it, but I don't think in your case that would make such a big difference (however indexing does). Always use InnoDB , unless your data is not important (for logs you can maybe ignore this).
  • Other DBMS behave basically like InnoDB . I'm currently using PostgreSQL , which is very mature, but definitely not as fast as MyISAM .

So, try to define good indexes. The query here needs an index on user_id, date . Note : The order is important!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM