1.如果a是int存float會自動四捨五入

insert into a (c)values(1.1)

2.亂數按照用戶看的影片類別推薦

select id,article_category_id from goblog.articles  where article_category_id=5 order by rand() limit 1

3.decimal ,float,double差別

宣告一個型別為 float 變數:
float a=1.234567f;
精確度可達 7 個數字
須於使用後置字元 f 或 F,
否則會將 double 的數值存放於變數內,

宣告一個型別為 double 變數:
double a=1.23456789012345;
精確度可達 15-16 個數字
如果要將整數當成 double ,須使用後置字元 d 或 D:
double b=3d;

宣告一個型別為 decimal 變數:
decimal a=6m;
精確度高達 28-29 個數字
須使用後置字元 m ,否則會將 double 的數值存放於變數內,

https://dotblogs.com.tw/cinthiea/2010/12/06/19947

4.創table時建index的句子

create table a(user_id int not null,cc_id int not null,key index_user_cc(user_id,cc_id))

5.索引優化器也會選錯,所以要判斷

 

MySQL索引选择不正确

一 表结构如下: 

CREATE TABLE t_audit_operate_log (
  Fid bigint(16) AUTO_INCREMENT,
  Fcreate_time int(10) unsigned NOT NULL DEFAULT '0',
  Fuser varchar(50) DEFAULT '',
  Fip bigint(16) DEFAULT NULL,
  Foperate_object_id bigint(20) DEFAULT '0',
  PRIMARY KEY (Fid),
  KEY indx_ctime (Fcreate_time),
  KEY indx_user (Fuser),
  KEY indx_objid (Foperate_object_id),
  KEY indx_ip (Fip)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

执行查询:

mysql> explain select count(*) from t_audit_operate_log where Fuser='XX@XX.com' and Fcreate_time>=1407081600 and Fcreate_time<=1407427199\G

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

id: 1

select_type: SIMPLE

table: t_audit_operate_log

type: ref

possible_keys: indx_ctime,indx_user

key: indx_user

key_len: 153

ref: const

rows: 2007326

Extra: Using where

 

发现,使用了一个不合适的索引, 不是很理想,于是改成指定索引:

mysql> explain select count(*) from t_audit_operate_log use index(indx_ctime) where Fuser='CY6016@cyou-inc.com' and Fcreate_time>=1407081600 and Fcreate_time<=1407427199\G

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

id: 1

select_type: SIMPLE

table: t_audit_operate_log

type: range

possible_keys: indx_ctime

key: indx_ctime

key_len: 5

ref: NULL

rows: 670092

Extra: Using where

实际执行耗时,后者比前者快了接近10

问题: 很奇怪,优化器为何不选择使用 indx_ctime 索引,而选择了明显会扫描更多行的 indx_user 索引。

分析2个索引的数据量如下:  两个条件的唯一性对比:

select count(*) from t_audit_operate_log where Fuser='XX@XX.com';
+----------+
| count(*) |
+----------+
| 1238382 | 
+----------+

select count(*) from t_audit_operate_log where Fcreate_time>=1407254400 and Fcreate_time<=1407427199;
+----------+
| count(*) |
+----------+
| 198920 | 
+----------+

显然,使用索引indx_ctime好于indx_user,但MySQL却选择了indx_user

 

http://blog.csdn.net/melody_mr/article/details/48950601

 

4.看cache是否有開起來

show variables like'query_cache_type'

開起來

set global query_cache_size=16777216 // Byte

set global query_cache_type=1

5.select 時用沒cache的語句

SELECT sql_no_cache count(*) FROM articles

5.show create table xxx 看create table的語句
 

 

arrow
arrow
    全站熱搜

    學習程式 發表在 痞客邦 留言(0) 人氣()