1. 简介
在Web应用程序体系架构中,数据持久层(通常是一个关系数据库)是关键的核心部分,它对系统的性能有非常重要的影响。MySQL是目前使用最多的开源数据库,但是MySQL数据库的默认设置性能非常的差,仅仅是一个玩具数据库。因此在产品中使用MySQL数据库必须进行必要的优化。
优化是一个复杂的任务,本文描述MySQL相关的数据库设计和查询优化,服务器端优化,存储引擎优化。
2. 数据库设计和查询优化
在MySQL
Server性能调优中,首先要考虑的就是Database Schema设计,这一点是非常重要的。一个糟糕的Schema设计即使在性能调优的MySQL
Server上运行,也会表现出很差的性能;和Schema相似,查询语句的设计也会影响MySQL的性能,应该避免写出低效的SQL查询。这一节将详细讨论这两方面的优化。
2.1 Schema Design
Schema的优化取决于将要运行什么样的query,不同的query会有不同的Schema优化方案。2.2节将介绍Query
Design的优化。Schema设计同样受到预期数据集大小的影响。Schema设计时主要考虑:标准化,数据类型,索引。
2.1.1 标准化
标准化是在数据库中组织数据的过程。其中包括,根据设计规则创建表并在这些表间建立关系;通过取消冗余度与不 ......
作者: 肖建彬
| 可以转载, 转载时务必以超链接形式标明文章原始出处
和作者信息及版权声明
网址:http://www.xiaojb.com/archives/it/mysqlroot.shtml
Method 1:
在/usr/local/mysql/bin/下:
./mysqladmin -u root password ‘new_password’
一般安装时用此方法设置。
Method 2:
在mysql状态下:
mysql>UPDATE user SET password=PASSWORD(’new_password’) WHERE user=’root’;
mysql>FLUSH PRIVILEGES;
Method 3:
mysql>SET PASSWORD FOR root=PASSWORD(’new_password’);
补一下补知道root密码情况下修改root密码
mysqld启动的时候加上–skip-grant-tables,然后马上修改密码,修改后去掉–skip-grant-tables,然后就OK了。
想起了3年前在eyou工作的时候,为了导qmail+vpopmail+mysql的邮件用户出来,修改了root密码,但是当时eyou的系统没有使用,原来的系统因为root密码而不能使用,我就看libvpopmail.a,从二进制文件中找到了密码:) ......
创建数据库:create database 数据库名;
创建表:create table 表名 ( 列名 数据类型 not null... );
查看系统中所有的数据库:show databases;
选择要应用的数据库:use 数据库名;
查看某个数据库下的所有表:show tables;
查看表结构:desc(describe) 表名;
重命名表名:alter table 表名 rename as 新表名
复制表结构:create table 表名 select * from 已存在的表名 where 1<>1;
复制表:create table 表名 select * from 已存在的表名 where 1=1;
删除数据库:drop database 数据库名;
备份数据库:mysqldump -h host -u root -p
给已存在的表添加列:alter table 表名 add column 列名 数据类型;
给已存在的表添加约束:
主键约束:alter table 表名 add constraint 约束名 primary key(列名);
唯一约束:alter table 表名 add constraint 约束名 unique(列名);
默认约束:alter table 表名 add constraint 约束名 default('默认内容') for 列名;
检查约束:alter table 表名 add constraint 约束名 check(约束条件);
外键约束:a ......
Mysql,SqlServer,Oracle主键自动增长的设置
1、把主键定义为自动增长标识符类型
在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如:
create table customers(id int auto_increment primary key not null, name varchar(15));
insert into customers(name) values("name1"),("name2");
select id from customers;
以上sql语句先创建了customers表,然后插入两条记录,在插入时仅仅设定了name字段的值。最后查询表中id字段,查询结果为:
id
1
2
由此可见,一旦把id设为auto_increment类型,mysql数据库会自动按递增的方式为主键赋值。
在MS SQLServer中,如果把表的主键设为identity类型,数据库就会自动为主键赋值。例如:
create table customers(id int identity(1,1) primary key not null, name varchar(15));
insert into customers(name) values("name1"),("name2");
select id from customers;
查询结果和mysql的一样。由此可见,一旦把id设为identity类型,MS SQLServer数据库会自动按递增的方式为主键赋值。identity包含两个参数,第一个参数表示起始值,第二个参数表示增量。
2、从序列中获取自动增长的标识 ......
Mysql,SqlServer,Oracle主键自动增长的设置
1、把主键定义为自动增长标识符类型
在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如:
create table customers(id int auto_increment primary key not null, name varchar(15));
insert into customers(name) values("name1"),("name2");
select id from customers;
以上sql语句先创建了customers表,然后插入两条记录,在插入时仅仅设定了name字段的值。最后查询表中id字段,查询结果为:
id
1
2
由此可见,一旦把id设为auto_increment类型,mysql数据库会自动按递增的方式为主键赋值。
在MS SQLServer中,如果把表的主键设为identity类型,数据库就会自动为主键赋值。例如:
create table customers(id int identity(1,1) primary key not null, name varchar(15));
insert into customers(name) values("name1"),("name2");
select id from customers;
查询结果和mysql的一样。由此可见,一旦把id设为identity类型,MS SQLServer数据库会自动按递增的方式为主键赋值。identity包含两个参数,第一个参数表示起始值,第二个参数表示增量。
2、从序列中获取自动增长的标识 ......
Mysql,SqlServer,Oracle主键自动增长的设置
1、把主键定义为自动增长标识符类型
在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如:
create table customers(id int auto_increment primary key not null, name varchar(15));
insert into customers(name) values("name1"),("name2");
select id from customers;
以上sql语句先创建了customers表,然后插入两条记录,在插入时仅仅设定了name字段的值。最后查询表中id字段,查询结果为:
id
1
2
由此可见,一旦把id设为auto_increment类型,mysql数据库会自动按递增的方式为主键赋值。
在MS SQLServer中,如果把表的主键设为identity类型,数据库就会自动为主键赋值。例如:
create table customers(id int identity(1,1) primary key not null, name varchar(15));
insert into customers(name) values("name1"),("name2");
select id from customers;
查询结果和mysql的一样。由此可见,一旦把id设为identity类型,MS SQLServer数据库会自动按递增的方式为主键赋值。identity包含两个参数,第一个参数表示起始值,第二个参数表示增量。
2、从序列中获取自动增长的标识 ......
1.reverse(str)函数: 返回颠倒字符顺序的字符串str, 该函数对多字节可靠的.
mysql> select * from user;
+----+------------------+
| id | name |
+----+------------------+
| 1 | test |
| 2 | test2 |
| 3 | abc cde aa bb_cc |
| 4 | abc |
+----+------------------+
4 rows in set (0.00 sec)
mysql> select id,reverse(name) name from user;
+----+------------------+
| id | name |
+----+------------------+
| 1 | tset |
| 2 | 2tset |
| 3 | cc_bb aa edc ......
之前写了一个自动解压压缩文件到压缩文件所在文件夹的脚本
后根据自己需要,写了另外两个。原理一样
都是使用winrar的命令
第一个脚本没考虑周到,只能解压rar文件
改进后可以支持winrar支持的各种文件
把指定文件夹下的文件保存到指定文件夹
#rardir.py
import os
import sys
src=sys.argv[1]
dst=sys.argv[2]
format=['rar','zip','7z','ace','arj','bz2','cab','gz','iso','jar','lzh','tar','uue','z']
os.chdir(sys.argv[1])
for file in os.listdir('.'):
if os.path.isfile(file) and (os.path.splitext(file)[1][1:].lower() in format)==True:
#cmd='winrar x -ibck "'+file+'" "'+dst+'\\'+os.path.splitext(file)[0]+'\\"'
cmd='winrar x -ibck "'+file+'" "'+dst+'\\"'
os.system(cmd)
os.remove(file)
print('done '+file)
第一个版本的改进
#rardecmp.py
#decompress with winrar
#arguments :filename directory opt
# opt='mkdir' to create directory with the correspond filename
# opt='direct' to decompress rar files in current directory
# opt='mk& ......