mysql 学习---->表优化、合成索引、浮点数与定点数

  1. 1.表优化  
  2. mysql> use test1;  
  3. Reading table information for completion of table and column names  
  4. You can turn off this feature to get a quicker startup with -A  
  5. Database changed  
  6.   
  7. mysql> alter table t rename t_old;  
  8.   
  9. mysql> create table t ( id varchar(100),content text);  
  10.   
  11. mysql> insert into t values  
  12.     -> (1,repeat('haha',100)),  
  13.     -> (2,repeat('test',100)),  
  14.     -> (3,repeat('java',100));  
  15.   
  16. mysql> insert into t select * from t;  
  17.   
  18. mysql> alter table t rename t_optimize;  

  19. mysql> select table_name,data_length from information_schema.tables where table_name = "t_optimize";  
  20. +------------+-------------+  
  21. | table_name | data_length |  
  22. +------------+-------------+  
  23. | t_optimize |       16384 |  
  24. +------------+-------------+  
  25.   
  26. mysql> optimize table t_optimize;  
  27.   
  28. +------------------+----------+----------+-------------------------------------------------------------------+  
  29. Table            | Op       | Msg_type | Msg_text                                                          |  
  30. +------------------+----------+----------+-------------------------------------------------------------------+  
  31. | test1.t_optimize | optimize | note     | Table does not support optimize, doing recreate + analyze instead |  
  32. | test1.t_optimize | optimize | status   | OK                                                                |  
  33. +------------------+----------+----------+-------------------------------------------------------------------+  
  34.   
  35. mysql> select table_name,data_length from information_schema.tables where table_name = "t_optimize";  
  36. +------------+-------------+  
  37. | table_name | data_length |  
  38. +------------+-------------+  
  39. | t_optimize |       16384 |  
  40. +------------+-------------+  
  41.  
  42. 2.合成索引  
  43. mysql> create table t (id varchar(100),context blob,hash_value varchar(40));  
  44.   
  45. mysql> insert into t values(1,repeat('zhejiang',2),md5(context));  
  46.   
  47. mysql> insert into t values(1,repeat('jiangsu',2),md5(context));  
  48.   
  49. mysql> insert into t values(1,repeat('fujian 2008',2),md5(context));  
  50.   
  51. mysql> select * from t;  
  52. +------+------------------------+----------------------------------+  
  53. | id   | context                | hash_value                       |  
  54. +------+------------------------+----------------------------------+  
  55. | 1    | zhejiangzhejiang       | 8f36854efa45585f35e6ca54b7d58f31 |  
  56. | 1    | jiangsujiangsu         | e2817db97256ac13805ee1ccfc1048e7 |  
  57. | 1    | fujian 2008fujian 2008 | 1efb8d993ee2cae7532407ff30b5778c |  
  58. +------+------------------------+----------------------------------+  
  59.   
  60. mysql> select * from t ;  
  61. +------+------------------------+----------------------------------+  
  62. | id   | context                | hash_value                       |  
  63. +------+------------------------+----------------------------------+  
  64. | 1    | zhejiangzhejiang       | 8f36854efa45585f35e6ca54b7d58f31 |  
  65. | 1    | jiangsujiangsu         | e2817db97256ac13805ee1ccfc1048e7 |  
  66. | 1    | fujian 2008fujian 2008 | 1efb8d993ee2cae7532407ff30b5778c |  
  67. +------+------------------------+----------------------------------+  
  68.   
  69. mysql> select * from t  
  70.     ->  where hash_value=md5(repeat('fujian 2008',2));  
  71. +------+------------------------+----------------------------------+  
  72. | id   | context                | hash_value                       |  
  73. +------+------------------------+----------------------------------+  
  74. | 1    | fujian 2008fujian 2008 | 1efb8d993ee2cae7532407ff30b5778c |  
  75. +------+------------------------+----------------------------------+  
  76.   
  77. mysql> desc t;  
  78. +------------+--------------+------+-----+---------+-------+  
  79. | Field      | Type         | Null | Key | Default | Extra |  
  80. +------------+--------------+------+-----+---------+-------+  
  81. | id         | varchar(100) | YES  |     | NULL    |       |  
  82. | context    | blob         | YES  |     | NULL    |       |  
  83. | hash_value | varchar(40)  | YES  |     | NULL    |       |  
  84. +------------+--------------+------+-----+---------+-------+  
  85.   
  86. mysql>  create index inx_blob on t(context(100));   
  87.   
  88. mysql> select * from t;  
  89. +------+------------------------+----------------------------------+  
  90. | id   | context                | hash_value                       |  
  91. +------+------------------------+----------------------------------+  
  92. | 1    | zhejiangzhejiang       | 8f36854efa45585f35e6ca54b7d58f31 |  
  93. | 1    | jiangsujiangsu         | e2817db97256ac13805ee1ccfc1048e7 |  
  94. | 1    | fujian 2008fujian 2008 | 1efb8d993ee2cae7532407ff30b5778c |  
  95. +------+------------------------+----------------------------------+  
  96.   
  97. mysql> desc select * from t where context like 'fujian%' \G;  
  98. *************************** 1. row ***************************  
  99.            id: 1  
  100.   select_type: SIMPLE  
  101.         table: t  
  102.          type: range  
  103. possible_keys: inx_blob  
  104.           key: inx_blob  
  105.       key_len: 103  
  106.           ref: NULL  
  107.          rows: 1  
  108.         Extra: Using where   
  109.   
  110. ERROR:   
  111. No query specified  
  112.   
  113. mysql> desc select * from t where context like '%fujian' \G;  
  114. *************************** 1. row ***************************  
  115.            id: 1  
  116.   select_type: SIMPLE  
  117.         table: t  
  118.          type: ALL  
  119. possible_keys: NULL  
  120.           keyNULL  
  121.       key_len: NULL  
  122.           ref: NULL  
  123.          rows: 3  
  124.         Extra: Using where  
  125. 1 row in set (0.00 sec)  
  126.   
  127. ERROR:   
  128. No query specified  
  129.   
  130. 3.浮点数、定点数  
  131. mysql> create table t(f float(8,1));   
  132.   
  133. mysql> desc t;  
  134. +-------+------------+------+-----+---------+-------+  
  135. | Field | Type       | Null | Key | Default | Extra |  
  136. +-------+------------+------+-----+---------+-------+  
  137. | f     | float(8,1) | YES  |     | NULL    |       |  
  138. +-------+------------+------+-----+---------+-------+  
  139.   
  140. mysql> insert into t values(1.23456);  
  141.   
  142. mysql> select * from t;  
  143. +------+  
  144. | f    |  
  145. +------+  
  146. |  1.2 |  
  147. +------+  
  148.   
  149. mysql> insert into t values(1.25456);  
  150.   
  151. mysql> select * from t;  
  152. +------+  
  153. | f    |  
  154. +------+  
  155. |  1.2 |  
  156. |  1.3 |  
  157. +------+ 
  158.   
  159. mysql> create table test(c1 float(10,2),c2 decimal(10,2));  
  160.   
  161. mysql> insert into test values(131072.32,131072.32);  
  162.   
  163. mysql> select * from test;  
  164. +-----------+-----------+  
  165. | c1        | c2        |  
  166. +-----------+-----------+  
  167. | 131072.31 | 131072.32 |  
  168. +-----------+-----------+  
  169.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值