简体   繁体   English

这个mysql / myisam表可以优化吗?

[英]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 我对除流量以外的所有字段都有一个复合主键,并在user_id和service上有一个索引

is it maby more efficient to store the traffic as a float? 将流量存储为浮点数是否更有效?

Also is MyIsam a good engine or should I use innodb? MyIsam还是一个不错的引擎,还是我应该使用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除非使用索引中的前“ n”个字段,否则不会使用复合索引:

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. MySQL可以将多列索引用于测试索引中所有列的查询,或者仅测试第一列,前两列,前三列等等的查询。 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. 至少要为每个重要字段创建一个单独的索引,尤其是date列。

As Alnitak points out, the question is how your indexes are defined. 正如Alnitak指出的,问题是如何定义索引。 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. 将traffic属性转换为float不会带来性能,也不应有所作为。
  • MyISAM is fast! MyISAM很快! InnoDB will be slower, because it has all its DB transaction overhead. InnoDB将变慢,因为它具有所有数据库事务开销。 But: It depends on what you want. 但是:这取决于您想要什么。 MyISAM can be really dangerous, because it can break always (Poweroffs, etc). MyISAM可能真的很危险,因为它总是会崩溃(断电等)。 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). 始终使用InnoDB ,除非您的数据不重要(对于日志,您可以忽略它)。
  • Other DBMS behave basically like InnoDB . 其他DBMS的行为基本上类似于InnoDB I'm currently using PostgreSQL , which is very mature, but definitely not as fast as MyISAM . 我当前使用的是PostgreSQL ,它非常成熟,但绝对不如MyISAM快。

So, try to define good indexes. 因此,尝试定义良好的索引。 The query here needs an index on user_id, date . 这里的查询需要在user_id, date上建立索引。 Note : The order is important! 注意 :顺序很重要!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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