mysql默认顺序,MySQL默认顺序取决于WHERE

I have a table like this:

CREATE TABLE IF NOT EXISTS `table_test` (

`id` mediumint(8) unsigned NOT NULL,

`country` enum('AF','AX','AL') DEFAULT NULL,

`number` tinyint(3) unsigned DEFAULT NULL,

`sort_order` double unsigned NOT NULL DEFAULT '0',

PRIMARY KEY (`id`),

KEY `country` (`country`),

KEY `id` (`id`,`country`)

) ENGINE=MEMORY DEFAULT CHARSET=latin1;

I have a table for which I changed the default ordering like:

ALTER TABLE test_table ORDER BY sort_order ASC;

This table is never updated and no records are removed or added during its life. This all seems to work so if I use the folowwing query:

SELECT * FROM test_table LIMIT 10

It returns the 10 records in the right order.

And even if I use:

SELECT * FROM test_table WHERE num=3

it returns the results in the right order.

But if I do

SELECT * FROM test_table WHERE country='AX'

It will return the results in reversed order.

Can someone tell me how this can happen?

解决方案

Specifying the ORDER BY on a table is just a help for the engine to speed up queries with the same order. It will not force mysql to always return the result with the same ordering.

"ORDER BY enables you to create the new table with the rows in a specific order. Note that the table does not remain in this order after inserts and deletes. This option is useful primarily when you know that you are mostly to query the rows in a certain order most of the time. By using this option after major changes to the table, you might be able to get higher performance. In some cases, it might make sorting easier for MySQL if the table is in order by the column that you want to order it by later. "

So you must use the ORDER BY expression in your queries too.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值