Archive

Archive for the ‘database’ Category

数据库备份导入乱码问题

May 30th, 2011 yakjuly No comments

备份数据库时需要知道原来数据库的文件编码格式是什么。

show variables like “%character%”

看到的结果可能是latin1可能是gbk可能是utf8 代表着你在原来数据存储文字编码的格式,在备份时需要设定–default-character-set。不设定则默认时utf8

mysql执行导出

mysqldump -uyakjulycom -pxxxxx -h mysql.yakjuly.com –default-character-set=latin1 yakjuly_com > yakjuly_com.sql

这样在yakjuly_com.sql文件中 的中文内容就能显示正确。

那么再导入另外一个数据库

本地的数据库设定了 character-set是uf8 如果直接source则在数据库中仍然显示乱码

原因是在yakjuly_com.sql内容的开头有定义

/*!40101 SET NAMES latin1 */;

把latin1替换为utf8 保存,然后再创建数据库source该文件或 执行

mysql -uroot -p –default-character-set=utf8 -f yakjuly_com < yakjuly_com.sql

这样数据库中的中文就显示正确了

 

Categories: database, 杂七杂八 Tags:

Syntax “is true” won’t match index

August 10th, 2010 yakjuly No comments

在试用query_reviewer插件 优化程序时发现,sql查询语句语法使用 “is true” 不会使用索引。当使用 “= true” 时则使用索引。

mysql> show index from users;
+——-+————+—————————+————–+————-+———–+————-+———-+——–+——+————+———+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+——-+————+—————————+————–+————-+———–+————-+———-+——–+——+————+———+
| users | 0 | PRIMARY | 1 | id | A | 601 | NULL | NULL | | BTREE | |
| users | 1 | index_users_on_saleman | 1 | saleman | A | 4 | NULL | NULL | YES | BTREE | |
| users | 1 | index_users_on_profile_id | 1 | profile_id | A | 601 | NULL | NULL | YES | BTREE | |
| users | 1 | index_users_on_manager | 1 | manager | A | 4 | NULL | NULL | YES | BTREE | |
| users | 1 | index_users_on_on_the_job | 1 | on_the_job | A | 4 | NULL | NULL | YES | BTREE | |
| users | 1 | index_users_on_service | 1 | service | A | 4 | NULL | NULL | YES | BTREE | |
+——-+————+—————————+————–+————-+———–+————-+———-+——–+——+————+———+

mysql> explain select * from users where saleman is true;
+—-+————-+——-+——+—————+——+———+——+——+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——-+——+—————+——+———+——+——+————-+
| 1 | SIMPLE | users | ALL | NULL | NULL | NULL | NULL | 601 | Using where |
+—-+————-+——-+——+—————+——+———+——+——+————-+
1 row in set (0.00 sec)

mysql> explain select * from users where saleman = true;
+—-+————-+——-+——+————————+————————+———+——-+——+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——-+——+————————+————————+———+——-+——+————-+
| 1 | SIMPLE | users | ref | index_users_on_saleman | index_users_on_saleman | 2 | const | 11 | Using where |
+—-+————-+——-+——+————————+————————+———+——-+——+————-+
1 row in set (0.00 sec)

Categories: database Tags: ,