V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
stanhou
V2EX  ›  MySQL

如何提高这个 QUERY3 的运行效率?

  •  
  •   stanhou · 2016-03-03 18:47:28 +08:00 · 1717 次点击
    这是一个创建于 2993 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Table articles:
    id|category_id|title|created|updated
    Index:
    ADD KEY `category_id` (`category_id`,`created`)

    My testing:

    query1

    SELECT sql_no_cache * FROM `articles` WHERE category_id=1 order by created limit 0,15
    Average time: 0.0003

    query2

    SELECT sql_no_cache * FROM `articles` WHERE category_id=1 order by updated limit 0,15
    Average time: 0.0019

    query3

    SELECT sql_no_cache * FROM `articles` WHERE category_id in (1,2,3) order by created limit 0,15
    Average time: 0.0018

    query4

    SELECT sql_no_cache * FROM `articles` WHERE category_id in (1,2,3) order by updated limit 0,15
    Average time: 0.0018

    created 做了二级索引, query1 明显用了索引(对比 query2 ),但是 query3 没用索引。请问如何索引提高 query3 的查询效率?

    realpg
        1
    realpg  
       2016-03-03 21:13:14 +08:00
    category_id create 联合索引
    category_id updated 联合索引

    注意顺序, category 在前
    stanhou
        2
    stanhou  
    OP
       2016-03-03 23:42:40 +08:00
    @realpg 你都没看清我问的是什么
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3329 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:43 · PVG 20:43 · LAX 05:43 · JFK 08:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.