【问题标题】:Extremely slow MySQL performanceMySQL 性能极慢
【发布时间】:2015-09-13 02:34:01
【问题描述】:

我有一个包含大约 500K 个作业的表,每个作业都有一个唯一的 ID,用作主键和一个状态,用于指示作业是挂起、完成还是失败。状态是一个不是键的整数。

我的问题是,我尝试根据状态选择作业的简单查询需要太多时间,超过 10 分钟。数据库中连接了大约 46 个线程,我也重新启动了,但对性能没有帮助。

我粘贴了表架构和我尝试在此处运行的查询: http://pastie.org/10416054

有没有什么办法可以找出瓶颈并优化表,这样就不用那么长时间了?

【问题讨论】:

    标签: mysql sql performance database-schema


    【解决方案1】:

    几个小时后,我会执行以下命令:

    CREATE INDEX idx_qry_status ON queries(status);
    

    由于您的查询正在执行表扫描,因此不使用任何索引。

    请参阅Create Index 上的手册页。

    之后的视觉效果,表格(不是性能方面):

    create table queries
    (   id bigint auto_increment primary key,
        status int null
        -- partial definition
    );
    insert queries (status) values (7),(2),(1),(4),(1),(5),(9),(11);
    
    CREATE INDEX idx_qry_status ON queries(status);
    show indexes from queries;
    +---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table   | Non_unique | Key_name       | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | queries |          0 | PRIMARY        |            1 | id          | A         |           8 |     NULL | NULL   |      | BTREE      |         |               |
    | queries |          1 | idx_qry_status |            1 | status      | A         |           8 |     NULL | NULL   | YES  | BTREE      |         |               |
    +---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    

    【讨论】:

    • 谢谢,我正在尝试我正在制作备份,我现在正在尝试。
    • 效果很好,查询时间从 11 分钟减少到不到 1 秒。我现在正在阅读一篇关于 MySQL 索引的有趣文章,其中解释了很多 dreamhost.com/blog/2013/08/27/mysql-indexing-basics
    猜你喜欢
    • 2016-08-10
    • 1970-01-01
    • 2022-12-21
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多