mysql表联合union优化为表连接join
话说之前用php写的一个统计跑得好好的,突然一天不出数据了,追其原因出在sql语句上,由于数据量增大加上服务器老旧,运行sql语句的时候出现内存不足的提示,遂决定优化sql语句使其不产生临时表。需求清晰,下面举例说明:
原sql语句:
SELECT COUNT( DISTINCT md5 ) from active AS a
WHERE a.md5 in ( select md5 from alive_20091220 union
select md5 from alive_20091221 union
select md5 from alive_20091222 union
select md5 from alive_20091223 )
优化后的sql语句:
SELECT COUNT( DISTINCT a.md5 ) from active AS a
LEFT OUTER JOIN alive_20091220 AS b ON a.md5 = b.md5
LEFT OUTER JOIN alive_20091221 AS c ON a.md5 = c.md5
LEFT OUTER JOIN alive_20091222 AS d ON a.md5 = d.md5
LEFT OUTER JOIN alive_20091223 AS e ON a.md5 = e.md5
相关文档:
PHP中的MYSQL常用函数总结
1、mysql_connect()-建立数据库连接
格式:
resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
例:
$conn = @mysql_connect("localhost", "username", "password") or dir( ......
1, MySQL profiling
mysql> set profiling = 1;
mysql> select count(*) from test;
mysql> show profiles;
mysql> show profile for query 1;
2,mysql 监控
http://bbs.linuxtone.org/thread-1854-1-1.html
http://code.google.com/p/mysql-cacti-templates/ -> Hig ......
Mysql Explain 详解
一.语法
explain < table_name >
例如: explain select * from t3 where id=3952602;
二.explain输出解释
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys &nb ......
数据库的一张表里面存了关于一台设备的备份配置信息,类型是bit,主要就是是否已经配置,设置的时候取值是true,false,还是0,1呢?
纠结了半天,动手就好了,是0,1,而非true,false,
更新语句如下
update table_a
set configBaked= 0
where id in (2,3,4)
......
mysql常用命令
关闭数据库
/data01/mysql/bin/mysqladmin -p shutdown
导出数据库
/usr/local/mysql/bin/mysqldump -opt -uroot -p -h10.15.0.45 bbsdb > 20070918.bbsdb.sql
导入数据库
/usr/local/mysql/bin/mysql target_db_name < backup-file.sql
限制只有内部ip可以链接
/u ......