易截截图软件、单文件、免安装、纯绿色、仅160KB

MYSQL分表优化


我们的项目中有好多不等于的情况。今天写这篇文章简单的分析一下怎么个优化法。
这里的分表逻辑是根据t_group表的user_name组的个数来分的。
因为这种情况单独user_name字段上的索引就属于烂索引。起不了啥名明显的效果。
1、试验PROCEDURE.
DELIMITER $$
DROP PROCEDURE `t_girl`.`sp_split_table`$$
CREATE PROCEDURE `t_girl`.`sp_split_table`()
BEGIN
declare done int default 0;
declare v_user_name varchar(20) default '';
declare v_table_name varchar(64) default '';
-- Get all users' name.
declare cur1 cursor for select user_name from t_group group by user_name;
-- Deal with error or warnings.
declare continue handler for 1329 set done = 1;
-- Open cursor.
open cur1;
while done <> 1
do 
    fetch cur1 into v_user_name;
    if not done then
      -- Get table name.
      set v_table_name = concat('t_group_',v_user_name);
      -- Create new extra table.
      set @stmt = concat('create table ',v_table_name,' like t_group');
      prepare s1 from @stmt;
      execute s1;
      drop prepare s1;
      -- Load data into it.
      set @stmt = concat('insert into ',v_table_name,' select * from t_group where user_name = ''',v_user_name,'''');
      prepare s1 from @stmt;
      execute s1;
      drop prepare s1;
    end if;
end while;
-- Close cursor.
close cur1;
-- Free variable from memory.
set @stmt = NULL;
END$$
DELIMITER ;
2、试验表。
我们用一个有一千万条记录的表来做测试。
mysql&gt; select count(*) from t_group;
+----------+
| count(*) |
+----------+
| 10388608 | 
+----------+
1 row in set (0.00 sec)
表结构。
mysql&gt; desc t_group;
+-------------+------------------+------+----


相关文档:

MySQL Master Slave Replication

MySQL本身没有提供replication failover的解决方案(见How can I use replication to provide redundancy or high availability?)
如何使Replication方案具有HA?
答案是MMM(MySQL Master-Master Replication Manager)
MMM对MySQL Master-Slave Replication绝对是一个很有益的补充!
引言
Master-Slave的数据库机构 ......

Redhat下 Apache, php, mysql的默认安装路径

apache:
如果采用RPM包安装,安装路径应在 /etc/httpd目录下
apache配置文件:/etc/httpd/conf/httpd.conf
Apache模块路径:/usr/sbin/apachectl
web目录:/var/www/html
如果采用源代码安装,一般默认安装在/usr/local/apache2目录下
php:
如果采用RPM包安装,安装路径应在 /etc/目录下
php的配置文件:/etc/php.ini ......

mysql函数

now()函数以`yyyy-mm-dd hh:mm:ss返回当前的日期时间,可以直接存到datetime字段中。
curdate()以’yyyy-mm-dd’的格式返回今天的日期,可以直接存到date字段中。
curtime()以’hh:mm:ss’的格式返回当前的时间,可以直接存到time字段中。 ......

MySQL程序剖析(Profiling)

我们将要详细的讲到MySQL的剖析(Profiling),因为它很少依赖于你的应用。应用和服务器级别的剖析有的时候都是有必要的。虽然应用级别的剖析可以给你整个应用性能的总揽。,但是对MySQL的剖析提供了信息是服务器级别所提供不了的。比如,对PHP代码进行剖析不会显示MySQL有多少行语句执行了。
 
与应用剖析一样,目标是 ......

MySql中常见的命令集

一、连接MYSQL。
格式: mysql -h主机地址 -u用户名 -p用户密码
1、连接到本机上的MYSQL。
首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root -p,回车后提示你输密码.注意用户名前可以有空格也可以没有空格,但是密码前必须没有空格,否则让你重新输入密码.
如果 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号