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

关于MySQL数据量增加变化不大时,一种分页的优化方案

关于分页的优化。
我们知道,在MySQL中分页很简单,直接LIMIT page_no,page_total 就可以了。
可是当记录数慢慢增大时,她就不那么好使了。
这里我们创建摘要表来记录页码和原表之间的关联。
下面为测试数据。
原表:
CREATE TABLE `t_group` (
  `id` int(11) NOT NULL auto_increment,
  `money` decimal(10,2) NOT NULL,
  `user_name` varchar(20) NOT NULL,
  `create_time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`),
  KEY `idx_combination1` (`user_name`,`money`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
原表总记录数:
mysql> select count(*) from t_group;
+----------+
| count(*) |
+----------+
| 10485760 |
+----------+
1 row in set (0.00 sec)
分页表:
CREATE TABLE `t_group_ids` (
  `id` int(11) NOT NULL,
  `group_id` int(11) NOT NULL,
  PRIMARY KEY  (`id`,`group_id`),
  KEY `idx_id` (`id`),
  KEY `idx_group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入分页表数据。当然这里如果你的表主键不是ID,那你得自己想办法搞这个分页表的数据了。这个好实现,就不说了。
mysql> insert into t_group_ids select ceil(id/20),id from t_group;
Query OK, 10485760 rows affected (2 min 56.19 sec)
Records: 10485760  Duplicates: 0  Warnings: 0
现在来看看对比数据。
用普通LIMIT来实现分页。
mysql> select * from t_group where 1 limit 20;
+----+--------+-----------+---------------------+
| id | money  | user_name | create_time         |
+----+--------+-----------+---------------------+
|  1 |  50.23 | david     | 2008-10-23 12:55:49 |
|  2 |  55.23 | livia     | 2008-10-23 10:02:09 |
|  3 | 100.83 | leo       | 2008-10-23 10:02:22 |
|  4 |  99.99 | lucy      | 2008-10-23 10:02:39 |
|  5 | 299.99 | simon     | 2008-10-23 10:02:52 |
|  6


相关文档:

mysql 安装

一、安装
#
yum -y install mysql-server
二、配置
# vi /etc/my.cnf  ← 编辑MySQL的配置文件
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility pack ......

MySQL 严格模式 sql_mode

虽然说我们尽量在写程序的时候控制插入到数据库的数据,而不要用数据库去判断数据的对错,但是有时候为了方便还是需要数据库自身的容错能力来帮助我们达到目的的。举例说明:
创建如下数据表
CREATE TABLE `book` (
  `id` int(11) default NULL,
  `num` int(11) unsigned default NULL
) ENGINE=InnoDB DE ......

让memcached和mysql更好的工作

http://www.itlearner.com/article/2009/4325.shtml
这次是Fotolog
的经验,传说中比Flickr更大的网站
,Fotolog在21台服务
器上部署了51个memcached实例,总计有254G缓存空间可用,缓存了多达175G的内容,这个数量比很多网站的数据库都要大的多,原文是A Bunch of Great Strategies for Using Memcached and MySQL Bet ......

MySQL语句优化的基本原则

MySQL语句优化的基本原则: 
◆1、使用索引来更快地遍历表。 
缺省情况下建立的索引是非群集索引,但有时它并不是最佳的。在非群集索引下,数据在物理上随机存放在数据页上。合理的索引设计要建立在对各种查询的分析和预测上。一般来说: 
a.有大量重复值、且经常有范围查询( > ,< ,> =,< ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号