????????????
???????????? ???????[ 2015/8/11 10:29:37 ] ????????????? ?????
??????????PHP????????????????????????????????????????????????????PHP?????????????????????????????????Щ????? PHP??????????????????????????????????????????????Щ???????????????????????迴???????????????????????????????? ??PHP??ó??????????????
?????Щ????λ?????????????????????????????????е???????棬???????????????? ???????г?????????????????????????MySQL????????????й??????????????????????????????????Щ???????????????????????? ?????????????????????????д????????μ??????
???????????????????????where??????ν??????????????????????????????????μ???
????CREATE TABLE mytable (
????id serial primary key??
????category_id int not null default 0??
????user_id int not null default 0??
????adddate int not null default 0
????);
??????????????????????????????????????????????????????????μ????
????SELECT * FROM mytable WHERE category_id=1;
???????????????????category_id?????????????????
????CREATE INDEX mytable_categoryid
????ON mytable (category_id);
????OK???????????????????в??????????????????磺
????SELECT * FROM mytable WHERE category_id=1 AND user_id=2;
??????????????????????user_id????????????????????????????????????????????????????
????CREATE INDEX mytable_categoryid_userid ON mytable (category_id??user_id);
??????????????????????????????"????_???1??_???2??"????????????????????????????
?????????????????????ν??????????????????????е???????????????????????????????Щ?????????????OK??????????????????????????????????????EXPLAIN????
????EXPLAIN
????SELECT * FROM mytable
????WHERE category_id=1 AND user_id=2;
????This is what Postgres 7.1 returns (exactly as I expected)
????NOTICE: QUERY PLAN:
????Index Scan using mytable_categoryid_userid on
????mytable (cost=0.00..2.02 rows=1 width=16)
????EXPLAIN
??????????postgres????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????????????????????????и?ORDER BY???????????????????????????????????order by?????????????????????檔
????SELECT * FROM mytable
????WHERE category_id=1 AND user_id=2
????ORDER BY adddate DESC;
?????е????????????????where????е???ν?????????????????ORDER BY??????е???ν????????????
????CREATE INDEX mytable_categoryid_userid_adddate
????ON mytable (category_id??user_id??adddate);
???????: "mytable_categoryid_userid_adddate" ????????
????"mytable_categoryid_userid_addda"
????CREATE
????EXPLAIN SELECT * FROM mytable
????WHERE category_id=1 AND user_id=2
????ORDER BY adddate DESC;
????NOTICE: QUERY PLAN:
????Sort (cost=2.03..2.03 rows=1 width=16)
????-> Index Scan using mytable_categoryid_userid_addda
????on mytable (cost=0.00..2.02 rows=1 width=16)
????EXPLAIN
????????EXPLAIN????????????е?????????????????????????????????????????????????????????????????????????????????????е??????????????????????????????ɡ?
???????????????????????????????????????????????????????????????????????????postgres???????????????????????????--?? ORDER BY????У?????where????е???Ρ????????????????????????????????????????????????????????????????κε??????????????????? ??postgres?????????Щ????????????
????EXPLAIN SELECT * FROM mytable
????WHERE category_id=1 AND user_id=2
????ORDER BY category_id DESC??user_id DESC??adddate DESC;
????NOTICE: QUERY PLAN:
????Index Scan Backward using
????mytable_categoryid_userid_addda on mytable
????(cost=0.00..2.02 rows=1 width=16)
????EXPLAIN
?????????????????????????????????????????????????????????濪???????????????κε?????
?????? ????????????????????????????????????????????????????????????????????????????????????????????????????罫??????? ??????????????where????????е??????????????????????????????????????????????????????????????????????????????????е????????? ????????????????Щ????????У????????????
??????????????????ò??????????????????????????????????????????????????EXPLAIN?????????????????????????е????????????????OK???????????????????????????????????????????????????????????
?????????????????????????????????o?????????????????????????????????????????????????????o?????????б??????????????where??????????????????????С?????????????????????б?????????????????
????????????????Щ?????????????????????????????????EXPLAIN??????????ж??÷??????????????????????????????Щ??? ????????????????????????????????????????????????????Щ????£?????????????δ???????????????????????????洢??????????? ??????????????????????????????????????????????顣
????????????????????????б???????????????????????????????????????????Щ????????????????MySQL????"OPTIMIZE TABLE"??
???????????????????????????????????????棬????????Щ????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11