排名 優化 mysql SQL調優之排名優化

2021-10-18 01:54:06 字數 2660 閱讀 2669

mysql> explain extended select t.rowno from

(select @rowno:=@rowno+1 as rowno,ur.customer_id as userid from t_hss_user_info_rank ur,

(select @rowno:=0) rno order by ur.give_pers_count desc,

ur.last_give_time desc

) t where t.userid= '1000010000000010';

| id | select_type | table    | type   | possible_keys | key       | key_len | ref   | rows   | filtered | extra |

| 1 | primary |    | ref    |   | | 152   | const   | 10   | 100.00 | using where |

| 2 | derived |    | system   | null       | null     | null   | null   | 1    | 100.00 | null |

| 2 | derived |   ur      | index   | null      | give_pers_count | 11    | null   | 311847  | 100.00 | using index |

| 3 | derived |   null    | null   | null        | null       | null   | null   | null   | null | no tables used |

4 rows in set, 1 warning (0.00 sec)

mysql> show create table t_hss_user_info_rank\g

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

table: t_hss_user_info_rank

create table: create table `t_hss_user_info_rank` (

`customer_id` varchar(50) not null comment '會員id',

`user_name` varchar(50) default null comment '會員名稱',

`phone_no` varchar(11) default null,

`give_count` int(10) default null comment '贈送份額',

`give_pers_count` int(10) default null comment '贈送人數',

`last_give_time` datetime default null comment '最後贈送時間',

`create_time` datetime default null comment '建立時間',

`modify_time` datetime default null comment '修改時間',

`scene_no` int(10) default null comment '贈送份額',

primary key (`customer_id`),

key `index_user_rank_customer_id` (`customer_id`,`give_count`,`last_give_time`),

key `give_pers_count` (`give_pers_count`,`last_give_time`)

) engine=innodb default charset=utf8

1 row in set (0.00 sec)

mysql> select t.rowno from (select @rowno:=@rowno+1 as rowno,ur.customer_id as userid from t_hss_user_info_rank ur,(select @rowno:=0) rno order by ur.give_pers_count desc, ur.last_give_time desc ) t where t.userid= '1000010000000010';

| rowno |

| 139 |

1 row in set (2.42 sec)

1、優化方式(一),利用整形檢索快的原理,將customer_id在臨時表裡轉化為整形;

select t.rowno from (select @rowno:=@rowno+1 as rowno,cast(ur.customer_id as unsigned) as userid from t_hss_user_info_rank ur,(select @rowno:=0) rno order by ur.give_pers_count asc, ur.last_give_time asc ) t where t.userid= '1000010000000010';

| rowno |

| 139 |

1 row in set (0.71 sec)

這表現出乙個問題,表結構設計不合理,能用unsigned int表示,為什麼用varchar?

2、優化方式(二),使用中間表,儲存排序結果,每天更新一次;

mysql sql語句效能調優

在做伺服器開發時,有時候對併發量有一定的要求,有時候影響速度的是某個sql語句,比如某個儲存過程。現在假設伺服器 執行過程中,某個sql執行比較緩慢,那如何進行優化呢?假如現在伺服器 執行如下sql儲存過程特別緩慢 call sp wplogin register 1,1,1,830000 2222...

Google排名優化經驗

針對與google排名,關鍵是內容好。當然內容好,能不能讓google知道,那就是技巧方面的問題了。根據許多經驗,在這裡與各位朋友分享一下幾個要點 針對於它的標題,我是這樣設計的 注 標題設計最好不好超過40個字元,也就是20個漢字。注 經過試驗表明,這個標籤放在 這個標籤上面比較好。d 外部連線 ...

mysql調優之schema優化

mysql的三正規化 1nf 第一正規化 字段不可分 2nf 第二正規化 有主鍵,非主鍵字段依賴主鍵 3nf 第三正規化 非主鍵字段不能相互依賴 解釋 1nf 原子性 字段不可再分,否則就不是關聯式資料庫 2nf 唯一性 乙個表只說明乙個事物 3nf 每列都與主鍵有直接關係,不存在傳遞依賴 在企業中...