sql語句的where條件順序和索引的順序

2021-07-22 21:21:47 字數 1910 閱讀 7794

create

table

`problem_submit` (

`id`

int(11) not

null auto_increment,

`status`

int(11) not

null,

`user_id`

int(11) not

null,

`username`

varchar(30) not

null,

`problem_id`

int(11) not

null,

`time_used`

int(11) not

null,

`memory_used`

int(11) not

null,

`submit_date` datetime not

null,

`ip`

char(15) not

null,

`compiler_id`

int(11) not

null,

`source` longtext not

null,

`code_length`

int(11) not

null,

`error_message` longtext,

`contest_id`

int(11) default

null,

primary

key (`id`,`submit_date`),

key`contest_id,problem_id,status 查詢考試問題狀態` (`contest_id`,`problem_id`,`status`) using btree,

key`problem_id 預設id降序` (`problem_id`,`id`) using btree comment 'id避免外部排序',

key`status 預設id降序` (`status`,`id`) using btree comment 'id避免外部排序',

key`compiler_id 預設id降序` (`compiler_id`,`id`) using btree comment 'id避免外部排序',

key`user_id,status 查詢使用者提交狀態` (`user_id`,`status`,`id`) using btree,

key`user_id 預設id降序` (`user_id`,`id`) using btree,

key`contest_id 預設id降序` (`contest_id`,`id`) using btree,

key`contest_id,problem_id 查詢考試問題` (`contest_id`,`problem_id`,`id`) using btree

) engine=myisam auto_increment=1060256

default charset=utf8;

select * from problem_submit where user_id=200 andstatus=1

select * from problem_submit wherestatus=1 and user_id=200

select * from problem_submit wherestatus=2 and user_id=4358 and compiler_id=1

select * from problem_submit wherestatus=2 and compiler_id=1 and user_id=4358

都會走索引,因為mysql會進行查詢的計畫優化

sql語句中的where條件執行順序

語句1 select from work order list where head corp id 1 and work order state in 1,2 and receiving id is null or receiving id 27 語句2 select from work orde...

sql語句 where 後條件執行先後順序

結論如下 針對mysql,其條件執行順序是 從左往右,自上而下 針對orcale,其條件執行順序是從右往左,自下而上 mysql where執行順序是從左往右執行的,在資料量小的時候不用考慮,但資料量多的時候要考慮條件的先後順序,此時應遵守乙個原則 排除越多的條件放在第乙個 在用mysql查詢資料庫...

sql語句where後條件執行順序與優化

針對mysql,其條件執行順序是 從左往右,自上而下 針對orcale,其條件執行順序是從右往左,自下而上。where執行順序是從左往右執行的,在資料量小的時候不用考慮,但資料量多的時候要考慮條件的先後順序,此時應遵守乙個原則 排除越多的條件放在第乙個。在用mysql查詢資料庫的時候,連線了很多個過...