MySQL Storage Engines(转)

十一月 13, 2009 – 11:33 上午

 Mike Peters, 01-20-2008

One of the greatest things about MySQL, other than being free, widely supported and fast, is the flexibility of choosing different storage engines for different tables.

Out of the box, MySQL comes with 7 storage engines, including an “example” stub storage engine that allows you to implement your own storage library.

What’s the big deal about having all these options?

Every storage engine is completely different, designed to address a unique application need.

Not being locked down to a single storage engine (like Oracle), means you can optimize and choose the best tool for the job.

Tip: A well designed MySQL-powered application will use different storage engines for different tables. If you’re still stuck with all MyISAM tables, now may be a good time to revisit.

MySQL Storage Engines Overview

MyISAM: The default engine. No transactions support, average data reliability. Offers great performance for read heavy applications. Most web services and data warehousing applications use MyISAM heavily.

HEAP: All in-memory. Very fast for data retrieval, however due to being stored only in memory - all data is lost on shutdown. Great for temporary tables.

Archive: Used for storing large amounts of data without indexes in a small footprint.

Merge: Collection of MyISAM tables logically merged together to provide a single view.

InnoDB: Transaction-safe storage engine, best suited for write heavy environments thanks to row-level locking. Offers good built-in recovery and solid data reliability. InnoDB engine was acquired by Oracle on 2005.

NDB: A clustered engine - data is automatically split and replicated across several machines, a.k.a data nodes. Best suited for applications that require high performance lookups with the highest possible degree of uptime and availability. Originally designed by Ericsson for the Telco market, NDB offers the highest levels of data reliability (99.999%). NDB works well in read heavy environments. For write heavy environments with multiple concurrent writes, consider InnoDB.

The biggest disadvantadge of NDB, is that by design your entire database must fit in memory. If your database size times 2 is too big to fit in memory, NDBCluster is not for you.

-

To make it easier to follow the unique characteristics of each storage engine, I created this magic quadrant diagram:


Examples:

Below are some examples of using the best storage engine for different tasks:

Search Engine - NDBCluster

Web stats logging - Flat file for the logging with an offline processing demon processing and writing all stats into InnoDB tables.

Financial Transactions - InnoDB

Session data - MyISAM or NDBCluster

Localized calculations - HEAP

Dictionary - MyISAM

Important notes about MyISAM tables:

1. Your tables will get corrupted eventually! Plan accordingly.

Tar the entire database directory daily and setup MySQL replication to a slave for an up-to-the-minute live backup.

2. Turn on auto-repair by adding this flag to your my.cnf file:
myisam-recover=backup,force

Or consider running a check-all-tables-and-email-me cronjob daily: See our MySQL Table Maintenance automation.

3. Super fast for read (select) operations.

4. Concurrent writes lock the entire table. Switch everything to offline processing where you can, to serialize writes without taking the database down. (Offline processing is golden and applies to all table types)

Important notes about HEAP/Memory tables:

While this type of table offers super fast retrieval, it only works well for small temporary tables.

If you try to load too much data into a Memory table, MySQL will start swapping information to disk and then you lose the benefits of an all-memory storage.

Important notes about InnoDB tables:

1. ACID transactions support. Row-level locking (compared to table level locking with MyISAM) means faster concurrent writes.

2. Doing a “SELECT Count(*) FROM table” without specifying any indexes is very slow on InnoDB and requires a full table scan. (With MyIsam this operation doesn’t cost anything because MyIsam stores an internal record counter with each table).

If you need to “SELECT COUNT(*)” often on InnoDB tables, create MySQL insert/delete triggers that will increment/decrement a counter whenever records are added or deleted from the table.

3. Backup:

Doing a tar/rsync backup where you simply copy all files is not possible with InnoDB.

MySQLDump backup is too slow with InnoDB. (If you insist on using it, turn on these flags: –opt –compress)

The only viable fast backup option, which can also be used to populate new slave machines, is InnoDB Hot Backup.

4. Recovery:

InnoDB has built-in recovery that works 99% of the times automatically. Never try to move .frm or .ibd files around as a way of “helping” the database to recover. If the built-in recovery doesn’t work, switch to your slave server and restore the primary from backup.

5. LOAD DATA INFILE is too slow with InnoDB. Consider using MyIsam tables for LOAD DATA operations.

6. InnoDB is less forgiving than MyIsam when it comes to queries on non indexes. InnoDB is going to “School” you into ensuring every single query and update statement runs on an index. Issue no index queries and you’ll pay dearly in execution time.

7. Never ever change my.cnf INnoDB log file size while the database is running. You’ll corrupt the log sequence number beyond repair.

8. To maximize InnoDB MySQL database performance, start with these my.cnf settings:

innodb_open_files = 500
innodb_file_per_table
innodb_buffer_pool_size = 250M
innodb_flush_log_at_trx_commit = 2
innodb_thread_concurrency =8
innodb_lock_wait_timeout = 500
interactive_timeout = 20
back_log = 75
table_cache = 300
thread_cache = 32
thread_concurrency = 8
wait_timeout = 30
connect_timeout = 10

9. If InnoDB crashes and the built-in recovery mechanism is unable to roll-back transactions, your database will not start. This is very important to understand. With MyISAM, even if a table gets corrupted, you can still start the database and everything will work normally. InnoDB will simply refuse to start until you restore the entire database from backup. Make sure you understand this principle and backup religiously.

Scalability:

Every successful web application eventually outgrows the capacity and throughput of a single database machine.

At that point you typically have two options - Replication or NDBCluster.

As always the choice depends on the needs of your application.

For read-heavy environments, use an NDBCluster or setup replication for n MyISAM slave read-only machines.

For write-heavy environments, InnoDB on an active/passive replication setup is typically the best choice. You may also want to experiment with an NDBCluster. An NDBCluster is generally going to be slower than InnoDB in write-heavy environments, but it offers a higher level of availability.

Helpful Scripts:

* Convert all tables to InnoDB

* MySQLDump one database to another (designed for a case where you cannot use InnoDB Hot backup and you have a lot of tables/databases to import. Script automatically retries, reopens connection and imports one table at a time)

* Add triggers to all tables for fast InnoDB count(*)
You can only have one delete/insert trigger per each table so if you’re already using triggers, modify the code accordingly.

freebcp的乱码问题

十一月 13, 2009 – 10:06 上午

最近在使用freetds的freebcp工具时候,经常会遇到的乱码问题。

这些乱码问题都是由于客户端和服务器端的字符集不一致引起的。

解决起来也很容易,只要修改成一样的就ok了。

在freebcp代码中的连接数据库部分增加修改字符集的代码,如下:

DBSETLCHARSET(login,”GBK”);

搞定。

如果想更加灵活,可以修改代码将字符集作为一个参数传给freebcp。

><

SQL SERVER中的分区表

十一月 6, 2009 – 5:03 下午

SQL SERVER中的分区表

SQL SERVER中的分区表和postgresql中的分区表创建方法类似。

1,创建PARTITION FUNCTION

1> CREATE PARTITION FUNCTION RangePF1 ( int )
2> AS RANGE FOR VALUES (10, 100, 1000) ;
3> GO

测试分区函数
1> SELECT $PARTITION.RangePF1 (10) ;
2> GO

2,创建PARTITION SCHEME

CREATE PARTITION SCHEME myRangePS1
AS PARTITION myRangePF1
TO (test1fg, test2fg, test3fg, test4fg) ;
GO

将几个分区分别映射到test1fg, test2fg, test3fg, test4fg四个文件组上

3,创建分区表

CREATE TABLE sunwg02 (col1 int, col2 char(10))
ON RangePF1 (col1) ;
GO

这样就创建了一张分区表,可以从sys.partitions中查看相关分区的信息

下面通过查看执行计划看看分区的效果

1> set showplan_text on
2> go
1> select * from sunwg02 where col1=1;
2> go
StmtText
select * from sunwg02 where col1=1;
(1 row affected)
StmtText
  |–Table Scan(OBJECT:([shxn].[robit].[sunwg02]),
  SEEK:([PtnId1001]=RangePartitionNew(CONVERT_IMPLICIT(int,[@1],0),(0),(10),(100),(1000))), 
  WHERE:([shxn].[robit].[sunwg02].[col1]=CONVERT_IMPLICIT(int,[@1],0)) ORDERED FORWARD)

SQL SERVER中常用管理SQL

十一月 6, 2009 – 4:43 下午

SQL SERVER中常用管理SQL

1,查询所有数据库信息
1> select * from sys.databases;
2> go

2,查询当前数据中的用户表
1> select * from INFORMATION_SCHEMA.tables;
2> go

或者
1> select * from sys.tables;
2> go

或者
1> select * from sys.all_objects where type_desc=’USER_TABLE’;
2> go

3,查询当前数据库中的系统表
1> select * from sys.all_objects where type_desc=’SYSTEM_TABLE’;
2> go

4,查询当前数据库中所有表
1> sp_tables
2> go
5,查询表中的字段信息

1> sp_columns all_objects
2> go

或者
1> select * from information_schema.columns where table_name=’sunwg01′;
2> go

6,查询数据库中的视图
1> select * from information_schema.views;
2> go

7,查询数据库中的索引情况
1> select * from sys.indexes;
2> go

8,查询数据库中的文件组
1> select * from sys.filegroups;
2> go

9,查询数据库中文件信息
1> select * from sys.sysfiles;
2> go

10,查询表所在的文件组
1> select * from sys.partitions where object_id=object_id(’sunwg01′);
2> go

1> select * from sys.allocation_units;
2> go

oracle调用c函数(续)

十一月 6, 2009 – 3:42 下午

oracle调用c函数(续)

本来挺简单的问题,可惜我们系统安装的有问题。
oracle是64位的,而gcc是32位的。
这样在调用的过程会报错误。

ERROR at line 1:
ORA-06520: PL/SQL: Error loading external library
ORA-06522: ld.so.1: extproc: fatal:
/opt/oracle/products/10.2.0/lib/sunwg.so: wrong ELF class: ELFCLASS32
ORA-06512: at “ETL.H_EXECUTE_CMD”, line 1

重装64位的gcc也是个麻烦事。

从网上查了半天终于发现个好办法。

修改LD_LIBRARY_PATH=/opt/oracle/products/10.2.0/lib为
LD_LIBRARY_PATH=/opt/oracle/products/10.2.0/lib32

这样调用32位的oracle lib库。
重新启动listener。

搞定。
><

Page 1 of 6412345678910»...Last »