mysql有索引比没索引查询速度还慢_mysql有索引,查询依然非常慢,请问怎么优化?...

SELECT

*

FROM

`order`

WHERE user_id = 1

ORDER BY `create_at` DESC

LIMIT 12 OFFSET 15000

表中有20万+数据。现在这条语句查询需要20秒。 当LIMIT 12 OFFSET 12 或者 24,36等等前面的页速度都还不错,但是随着页数的增加,也就是OFFSET变化,越大越慢。

表id(主键,自增),user_id,create_at都增加了索引。

请问应该如何解决这个问题。

这里的where 条件只列了一个, 实际上可能还有更多的可能性。如果有更多的where又该如何?

orderby 目前肯定是针对已经有索引页的字段进行排序的,但是也有3个,时间字段。

谢谢。

补充

//ddl

CREATE TABLE `foobar` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`identdify` bigint(20) DEFAULT '0',

`type` int(11) DEFAULT '0',

`status` int(11) DEFAULT '0',

`way` int(11) DEFAULT '0',

`api` int(11) DEFAULT '0',

`node` int(11) DEFAULT '0',

`apply_by` int(11) DEFAULT '0',

`apply_at` int(11) DEFAULT '0',

`create_by` int(11) DEFAULT '0',

`create_at` int(11) DEFAULT '0',

`confirm_by` int(11) DEFAULT '0',

`confirm_at` int(11) DEFAULT '0',

`check_by` int(11) DEFAULT '0',

`check_at` int(11) DEFAULT '0',

`money_1` decimal(20,2) DEFAULT '0.00',

`money_2` decimal(20,2) DEFAULT '0.00',

`money_3` decimal(20,2) DEFAULT '0.00',

`money_4` decimal(20,2) DEFAULT '0.00',

`money_5` decimal(20,2) DEFAULT '0.00',

`money_6` decimal(20,2) DEFAULT '0.00',

`money_7` decimal(20,2) DEFAULT '0.00',

`money_8` decimal(20,2) DEFAULT '0.00',

`api_identify` varchar(255) DEFAULT NULL,

`client_name` varchar(255) DEFAULT NULL,

`remark` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `type` (`type`),

KEY `status` (`status`),

KEY `way` (`way`),

KEY `node` (`node`),

KEY `apply_by` (`apply_by`),

KEY `apply_at` (`apply_at`),

KEY `create_by` (`create_by`),

KEY `create_at` (`create_at`),

KEY `confirm_by` (`confirm_by`),

KEY `create_at_2` (`create_at`)

) ENGINE=MyISAM AUTO_INCREMENT=251720 DEFAULT CHARSET=utf8

SELECT count(*) FROM `foobar`;

+----------+

| count(*) |

+----------+

| 251719 |

+----------+

1 row in set (0.00 sec)

SELECT

*

FROM

`foobar`

WHERE way = 1

ORDER BY create_at DESC

LIMIT 12 OFFSET 20000

// 耗时 25.890 秒

EXPLAIN

-> SELECT

-> *

-> FROM

-> `foobar`

-> WHERE way = 1

-> ORDER BY create_at DESC

-> LIMIT 12 OFFSET 20000 \G;

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: foobar

type: range

possible_keys: way

key: way

key_len: 5

ref: NULL

rows: 251028

Extra: Using index condition; Using filesort

1 row in set (0.00 sec)

SELECT

*

FROM

`foobar`

WHERE way = 1

AND api = 1

ORDER BY create_at DESC

LIMIT 12 OFFSET 20000

//耗时 24.585

> EXPLAIN

-> SELECT

-> *

-> FROM

-> `foobar`

-> WHERE way = 1

-> AND api = 1

-> ORDER BY create_at DESC

-> LIMIT 12 OFFSET 20000 \G;

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: foobar

type: range

possible_keys: way

key: way

key_len: 5

ref: NULL

rows: 251028

Extra: Using index condition; Using where; Using filesort

1 row in set (0.00 sec)

ERROR: No query specified

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值