分类: Mysql/postgreSQL
2008-02-01 17:05:38
通常的数据库设计指南和规范式会提高性能。还有一些其他的技巧,本章主要内容针对其他的技巧,如下:
* mysql 数据库为什么慢
* 选择合适的设计
* 使用索引实现优化
* 使用OPTIMIZE TABLE
* 没有使用足够的索引。查询很慢
* 使用过多的索引。插入,更新,删除时重建索引需要太多时间。
*
使用table- and column-level privileges
*
使用错误的数据库设计策略
* 尽量使用小数据类型
* 尽量使用固定长度记录。即尽量不使用VARCHAR, no TEXT, and no BLOB.对于TEXT和BLOB,尽量拆作单独表。尽量用CHAR代替VARCHAR.
* 为列尽可能多使用“NOT NULL”
* table-by-table basis,非事务相关的表,比如MyISAM,开销比较小。Mysql中同一数据库可以使用不同类型的表的。
* 选择合适的索引,下一节描述
* 有时不要规范式,它会导致太多的连接,维护起来麻烦。
PRIMARY, KEY, or UNIQUE都会产生索引。
We can use the ANALYZE TABLE statement to review and store the key distribution in a table.
mysql> analyze table vod_programs
-> ;
+-------------------+---------+----------+-----------------------------+
| Table | Op | Msg_type | Msg_text |
+-------------------+---------+----------+-----------------------------+
| mddb.vod_programs | analyze | status | Table is already up to date |
+-------------------+---------+----------+-----------------------------+
1 row in set (0.02 sec)
对存储进行优化,防止碎片
This command works only on MyISAM and BDB tables at the time of writing.
mysql> OPTIMIZE TABLE srv_plans;
+----------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+----------------+----------+----------+----------+
| mddb.srv_plans | optimize | status | OK |
+----------------+----------+----------+----------+
1 row in set (0.03 sec)
· You can optimize your database structure by keeping data a small and fixed size, by indexing appropriately, and by choosing an appropriate table type.
· Indexes are used to find records quickly according to the index column value.
· An index will be used in a query when the query is based on the index column or columns or a leftmost prefix of the index columns.
· OPTIMIZE TABLE tablename; performs housekeeping similar to defragging your disk.
chinaunix网友2008-02-01 17:06:18
§18.7 习题 Which of the following statements about indexes is true? Indexes take up too much space on disk and should not be used. Not having any indexes can make your queries run slowly. You should index as many columns as possible. None of the above. 2: When choosing data types for columns use the same ones throughout a database because this will make the database more efficient use variable-sized ones wherever possible to optimize disk usage use fixed-sized ones where