根据Chaos Wang的PPT整理而成, 在此再次感谢Chaos Wang的此次TechTalk
基本概念
• 字符(Character)是指人类语言中最小的表义符号。例如'A'、'B'等;
• 给定一系列字符,对每个字符赋予一个数值,用数值来代表对应的字符,这一数值就是字符的编码(Encoding)。例如,我们给字符'A'赋予数值0,给字符'B'赋予数值1,则0就是字符'A'的编码;
• 给定一系列字符并赋予对应的编码后,所有这些字符和编码对组成的集合就是字符集(Character Set)。例如,给定字符列表为{'A','B'}时,{'A'=>0, ‘B'=>1}就是一个字符集;
• 字符序(Collation)是指在同一字符集内字符之间的比较规则;
• 确定字符序后,才能在一个字符集上定义什么是等价的字符,以及字符之间的大小关系;
• 每个字符序唯一对应一种字符集,但一个字符集可以对应多种字符序,其中有一个是默认字符序(Default Collation);
• MySQL中的字符序名称遵从命名惯例:以字符序对应的字符集名称开头;以_ci(表示大小写不敏感)、_cs(表示大小写敏感)或_bin(表示按编码值比较)结尾。例如:在字符序"utf8_general_ci"下,字符"a"和"A"是等价的;
MySQL字符集设置• 系统变 ......
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显。关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情。当我们去设计数据库表结构,对操作数据库时(尤其是查表时的SQL语句),我们都需要注意数据操作的性能。这里,我们不会讲过多的SQL语句的优化,而只是针对MySQL这一Web应用最多的数据库。希望下面的这些优化技巧对你有用。
1. 为查询缓存优化你的查询
大多数的MySQL服务器都开启了查询缓存。这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的。当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一个缓存中,这样,后续的相同的查询就不用操作表而直接访问缓存结果了。
这里最主要的问题是,对于程序员来说,这个事情是很容易被忽略的。因为,我们某些查询语句会让MySQL不使用缓存。请看下面的示例:
上面两条SQL语句的差别就是 CURDATE() ,MySQL的查询缓存对这个函数不起作用。所以,像 NOW() 和 RAND() 或是其它的诸如此类的SQL函数都不会开启查询缓存,因为这些函数的返回是会不定的易变的。所以,你所需要的就是用一个变量来代替MySQL的函数,从而开启缓存。
2. EXPLAIN ......
tomcat 安装目录下的conf目录中的context.xml与web.xml文件分别修改如下:
context.xml 新加如下内容:
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://数据库地址:3306/数据库名称?autoReconnect=true"
username="数据库用户名"
password="数据库密码"
maxActive="30"
maxIdle="30"
maxWait="10000"
removeAbandoned="true"
removeAbandonedTimeout="300"
logAbandoned="true"/>
web.xml 新增如下内容
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref> ......
<?php
class mysql {
/*+++++++++++++++数据库访问类++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
//=================================================
//连接数据
private $Host = "localhost"; //服务器地址
private $Database = "sansu"; //数据库名称
private $User = "root"; //用户名
private $Password = "czq339403229"; //用户密码
private $Char_Set = "utf8"; //设置数据库字符集
//结果数据
public $Link_ID = 0; & ......
在FC8中默认安装的有mysql,没有的话可以很方便的安装下。
默认的mysql的include文件目录在/usr/include/mysql
默认的mysql的lib文件夹在/usr/lib/mysql
这两个目录在我们编译时候需要到。
我的测试用的C代码为:
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
#define CONN_HOST "localhost"
#define CONN_USER "root"
#define CONN_PASS "123"
#define CONN_DB "test"
int main()
{
MYSQL *conn;
char *server = CONN_HOST;
char *user = CONN_USER;
char *pass = CONN_PADD;
char *database = CONN_DB;
conn = mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,pass,database,0,NULL,0))
{
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
else
&nb ......
在FC8中默认安装的有mysql,没有的话可以很方便的安装下。
默认的mysql的include文件目录在/usr/include/mysql
默认的mysql的lib文件夹在/usr/lib/mysql
这两个目录在我们编译时候需要到。
我的测试用的C代码为:
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
#define CONN_HOST "localhost"
#define CONN_USER "root"
#define CONN_PASS "123"
#define CONN_DB "test"
int main()
{
MYSQL *conn;
char *server = CONN_HOST;
char *user = CONN_USER;
char *pass = CONN_PADD;
char *database = CONN_DB;
conn = mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,pass,database,0,NULL,0))
{
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
else
&nb ......
在FC8中默认安装的有mysql,没有的话可以很方便的安装下。
默认的mysql的include文件目录在/usr/include/mysql
默认的mysql的lib文件夹在/usr/lib/mysql
这两个目录在我们编译时候需要到。
我的测试用的C代码为:
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
#define CONN_HOST "localhost"
#define CONN_USER "root"
#define CONN_PASS "123"
#define CONN_DB "test"
int main()
{
MYSQL *conn;
char *server = CONN_HOST;
char *user = CONN_USER;
char *pass = CONN_PADD;
char *database = CONN_DB;
conn = mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,pass,database,0,NULL,0))
{
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
else
&nb ......
mysql> create table testdate(
-> id int not null auto_increment primary key,
-> time date);
Query OK, 0 rows affected (0.30 sec)
mysql> insert into testdate(time) values('2010-4-23');
Query OK, 1 row affected (0.06 sec)
mysql> select * from testdate;
+----+------------+
| id | time |
+----+------------+
| 1 | 2010-4-23|
+----+------------+
1 row in set (0.00 sec)
mysql> alter table testdate add column current time;
Query OK, 1 row affected (0.25 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> update testdate set current='21:18:00' where id=1;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> selec ......
mysql> create table testdate(
-> id int not null auto_increment primary key,
-> time date);
Query OK, 0 rows affected (0.30 sec)
mysql> insert into testdate(time) values('2010-4-23');
Query OK, 1 row affected (0.06 sec)
mysql> select * from testdate;
+----+------------+
| id | time |
+----+------------+
| 1 | 2010-4-23|
+----+------------+
1 row in set (0.00 sec)
mysql> alter table testdate add column current time;
Query OK, 1 row affected (0.25 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> update testdate set current='21:18:00' where id=1;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> selec ......