cocos

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  24 随笔 :: 0 文章 :: 1 评论 :: 0 引用

公告

2011年5月6日 #

DELIMITER $$
USE `servant_591up`$$
DROP PROCEDURE IF EXISTS `sp_move_data`$$
CREATE  PROCEDURE `sp_move_data`()
BEGIN
DECLARE v_exit INT DEFAULT 0;
DECLARE v_spid BIGINT;
DECLARE v_id BIGINT;
DECLARE i INT DEFAULT 0;
DECLARE c_table INT;
DECLARE v_UniqueKey VARCHAR(57);
DECLARE v_TagCatalogId INT;
DECLARE v_RootCatalogId INT;
DECLARE v_UserId BIGINT;
DECLARE v_QuestionId CHAR(36);
DECLARE v_CorrectCount  INT;
DECLARE v_ErrorCount INT;
DECLARE v_LastIsCorrect INT;
DECLARE v_LastAnswerXML TEXT CHARSET utf8;
DECLARE v_TotalCostTime INT;
DECLARE v_Reviews VARCHAR(200) CHARSET utf8;
DECLARE v_AnswerResultCategory INT;
DECLARE v_LastCostTime INT;
DECLARE v_LastAnswerTime DATETIME;
DECLARE v_IsPublic INT;
DECLARE v_SUBJECT INT;
DECLARE v_TotalCount INT;
DECLARE v_AnswerMode SMALLINT(6);
DECLARE v_ExerciseWeight FLOAT;
DECLARE c_ids CURSOR FOR SELECT UniqueKey,TagCatalogId,RootCatalogId,UserId,QuestionId,CorrectCount,ErrorCount,LastIsCorrect,LastAnswerXML,TotalCostTime,Reviews,AnswerResultCategory,LastCostTime,LastAnswerTime,IsPublic,SUBJECT,TotalCount,AnswerMode,ExerciseWeight FROM ol_answerresult_56;
DECLARE  CONTINUE HANDLER FOR NOT FOUND SET v_exit=1;
OPEN c_ids;
REPEAT
FETCH c_ids INTO v_UniqueKey,v_TagCatalogId,v_RootCatalogId,v_UserId,v_QuestionId,v_CorrectCount,v_ErrorCount,v_LastIsCorrect,v_LastAnswerXML,v_TotalCostTime,v_Reviews,v_AnswerResultCategory,v_LastCostTime,v_LastAnswerTime,v_IsPublic,v_SUBJECT,v_TotalCount,v_AnswerMode,v_ExerciseWeight;
IF v_exit = 0 THEN
SET @vv_id = v_id;
SELECT MOD(v_UserId,100) INTO c_table;
SET @SQL_CONTEXT =
CONCAT('INSERT INTO new_answerresult_',
c_table,'
(UniqueKey,TagCatalogId,RootCatalogId,UserId,QuestionId,CorrectCount,ErrorCount,LastIsCorrect,LastAnswerXML,TotalCostTime,Reviews,AnswerResultCategory,LastCostTime,LastAnswerTime,IsPublic,SUBJECT,TotalCount,AnswerMode,ExerciseWeight)values(',
'''',v_UniqueKey,'''',',',
v_TagCatalogId,',',
v_RootCatalogId,',',
v_UserId,',',
'''',v_QuestionId,'''',',',
v_CorrectCount,',',
v_ErrorCount,',',
v_LastIsCorrect,',',
'''',v_LastAnswerXML,'''',',',
v_TotalCostTime,',',
'''',REPLACE(IFNULL(v_Reviews,''),'''',''),'''',',',
v_AnswerResultCategory,',',
v_LastCostTime,',',
'''',v_LastAnswerTime,'''',',',
v_IsPublic,',',
v_SUBJECT,',',
v_TotalCount,',',
v_AnswerMode,',',
v_ExerciseWeight,')');
PREPARE STMT FROM @SQL_CONTEXT;
EXECUTE STMT ;
DEALLOCATE PREPARE STMT;
END IF;
SET i=i+1;
#100
#IF MOD(i,100)=0 THEN COMMIT;
#END IF;
UNTIL v_exit=1
END REPEAT;
CLOSE c_ids;
#COMMIT;
END$$
DELIMITER ;
posted @ 2011-05-06 22:53 大柳树 阅读(85) 评论(0) 编辑

LOAD DATA LOCAL INFILE 'c:\userlist.txt' INTO TABLE temp_user
FIELDS ESCAPED BY '\\' TERMINATED BY '\t' LINES TERMINATED BY '\r\n' (`username`, `userid`)
CREATE TABLE tc_userinfo(`username` VARCHAR(100),`ip` VARCHAR(100))
ENGINE = MYISAM DEFAULT CHARSET=utf8
SELECT * FROM tc_userinfo
ORDER BY papertimes DESC
1.关联userid
UPDATE tc_userinfo t ,ol_user u SET t.userid = u.userid WHERE t.username = u.username
/*truncate table ol_ip_tongchao*/
SELECT INET_ATON('123.15.57.50')
SELECT * FROM ol_ip_tongchao WHERE startip<2064595250 AND endip>2064595250
2.ip数据
LOAD DATA LOCAL INFILE 'c:\ip01.txt' INTO TABLE `servant_591up`.`ol_ip_tongchao`
FIELDS ESCAPED BY '\\' TERMINATED BY ',' LINES TERMINATED BY '\r\n' (`startip`, `endip`, `pos`,`detail`)
SELECT COUNT(*) FROM ol_ip_tongchao
TRUNCATE TABLE ol_ip_tongchao
SELECT * FROM ol_ip_tongchao LIMIT 100
SELECT INET_ATON('114.229.74.103')
UPDATE tc_userinfo t ,ol_ip_tongchao i SET t.diqu = i.pos WHERE INET_ATON(t.ip)>=i.startip AND INET_ATON(t.ip)<=i.endip
SELECT * FROM ol_ip_tongchao WHERE INET_ATON('114.229.74.103')
>startip AND INET_ATON('114.229.74.103')< endip
3.
UPDATE tc_userinfo t SET t.racetimes = 
(SELECT COUNT(*) FROM gwy_raceresult WHERE userid = t.userid)
4.
UPDATE tc_userinfo t SET t.papertimes = 
(SELECT COUNT(*) FROM ol_simulatelog WHERE userid = t.userid)
5.
UPDATE tc_userinfo t SET t.questiontimes = 
(SELECT COUNT(*) FROM ol_answerlog WHERE userid = t.userid)
6.
EXPLAIN
UPDATE tc_userinfo t SET t.wrongtimes = 
(SELECT COUNT(*) FROM ol_answerlog_tmp WHERE userid = t.userid AND IsCorrect = 0)
UPDATE tc_userinfo t , ol_answerlog l SET t.wrongtimes = 1
WHERE t.userid = l.userid AND l.IsCorrect = 0
CREATE TABLE ol_answerlog_tmp
SELECT * FROM ol_answerlog WHERE userid IN(SELECT userid FROM tc_userinfo)
7.
UPDATE tc_userinfo t SET t.rewrongtimes = ( SELECT 
COUNT(*)  FROM ol_tc_wrong WHERE userid = t.userid )
9.
UPDATE tc_userinfo t SET t.reviews = ( SELECT 
COUNT(*)  FROM ol_userreviews WHERE userid = t.userid AND Reviews IS NOT NULL AND Reviews<>'' )
SELECT * FROM ol_userreviews  WHERE Reviews IS NOT NULL AND Reviews<>''LIMIT 100 
posted @ 2011-05-06 22:52 大柳树 阅读(19) 评论(0) 编辑

posted @ 2011-05-06 22:50 大柳树 阅读(1654) 评论(1) 编辑

mysql_upgrade should be executed each time you upgrade MySQL. It checks all tables in all databases for incompatibilities with the current version of MySQL Server. If a table is found to have a possible incompatibility, it is checked. If any problems are found, the table is repaired. mysql_upgrade also upgrades the system tables so that you can take advantage of new privileges or capabilities that might have been added.
All checked and repaired tables are marked with the current MySQL version number. This ensures that next time you run mysql_upgrade with the same version of the server, it can tell whether there is any need to check or repair the table again.
mysql_upgrade also saves the MySQL version number in a file named mysql_upgrade.info in the data directory. This is used to quickly check if all tables have been checked for this release so that table-checking can be skipped.
To check and repair tables and to upgrade the system tables, mysql_upgrade executes the following commands:
mysqlcheck --check-upgrade --all-databases --auto-repair mysql_fix_privilege_tables
mysql_upgrade currently works only on Unix.
To use mysql_upgrade, make sure that the server is running, and then invoke it like this:
shell> mysql_upgrade 
posted @ 2011-05-06 22:38 大柳树 阅读(34) 评论(0) 编辑

EXPLAIN 
SELECT COUNT(*) FROM ol_answerresult_1 INNER JOIN ol_question ON ol_answerresult_1.QuestionId = ol_question.QuestionId   AND PaperId='00000000-0000-0000-0000-000000000000' AND ol_question.IsConfirmed=1 AND QuestionCategory=1  AND ol_question.SimilarType<>2 AND (GKYear!=0 OR GKArea!=0) WHERE ol_answerresult_1.userid=39176 AND ol_answerresult_1.SUBJECT=10
EXPLAIN
SELECT SQL_NO_CACHE questionid,AllTagCatalogId 
FROM ol_question WHERE MATCH(AllTagCatalogId)AGAINST (7117,7116);
EXPLAIN
MATCH() AGAINST
SELECT SQL_NO_CACHE questionid
FROM ol_question WHERE AllTagCatalogId LIKE '%7116%';
SELECT BENCHMARK(1000000,(SELECT SQL_NO_CACHE questionid FROM ol_question WHERE MATCH(AllTagCatalogId)AGAINST ('7116') LIMIT 1))
SELECT BENCHMARK(1000000,(SELECT SQL_NO_CACHE questionid FROM ol_question WHERE AllTagCatalogId LIKE '%7116%' LIMIT 1))
SELECT BENCHMARK(10000000,CURRENT_DATE())
posted @ 2011-05-06 22:37 大柳树 阅读(19) 评论(0) 编辑

mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=200 --iterations=1 --number-int-cols=4 --auto-generate-sql --auto-generate-sql-load-type=mixed,update,write,key,read --engine=myisam --number-of-queries=1000 -uroot -p8319999
mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=200 --iterations=10 --number-int-cols=4 --auto-generate-sql --auto-generate-sql-load-type=update --engine=myisam --number-of-queries=10000 -uroot -p8319999
mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=100 --iterations=10 --number-int-cols=4 --auto-generate-sql --auto-generate-sql-load-type=write --engine=myisam,innodb --number-of-queries=5000 -uroot -p8319999
mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=100 --iterations=10 --number-int-cols=4 --auto-generate-sql-guid-primary --auto-generate-sql --auto-generate-sql-load-type=write --engine=myisam --number-of-queries=5000 -uroot -p8319999
mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=10 --iterations=10  --auto-generate-sql-guid-primary --auto-generate-sql --auto-generate-sql-load-type=write --engine=myisam --number-of-queries=50 -uroot -p8319999
mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=200 --iterations=1 --number-int-cols=4 --auto-generate-sql --auto-generate-sql-load-type=mixed,update,write,key,read --engine=myisam --number-of-queries=10000 -uroot -p8319999
mysqlslap  --concurrency=50 --iterations=1 --create-schema=gwy_591up_237 --query="INSERT INTO ol_answerresult_test (UniqueKey, TagCatalogId, RootCatalogId, UserId, QuestionId, CorrectCount, ErrorCount, LastIsCorrect, LastAnswerXML, TotalCostTime, Reviews, AnswerResultCategory, LastCostTime, LastAnswerTime, IsPublic, SUBJECT, TotalCount, AnswerMode, ExerciseWeight ) VALUES ( '3313_67bd0665-3d82-4622-9bf0-c4bb36fd2cd6', 7122, '7115', 3313, '67bd0665-3d82-4622-9bf0-c4bb36fd2cd6', '1', '1', '0', '<as><a><i><![CDATA[B]]></i></a></as>', '25', '', '1', '25', '2011-04-21 21:18:49', '1', '10', '1', '0', '0' )" --number-of-queries=10000 -uroot -h192.168.56.221 -p888888
mysqlslap  --concurrency=1 --iterations=10 --create-schema=gwy_591up_237 --query="INSERT INTO ol_answerresult_test (UniqueKey, TagCatalogId,RootCatalogId,UserId,QuestionId,CorrectCount,ErrorCount, LastIsCorrect,LastAnswerXML,TotalCostTime,Reviews, AnswerResultCategory, LastCostTime, LastAnswerTime,IsPublic,SUBJECT,TotalCount,AnswerMode,ExerciseWeight)VALUES('3313_67bd0665-3d82-4622-9bf0-c4bb36fd2cd6', 7122, '7115', 3313, '67bd0665-3d82-4622-9bf0-c4bb36fd2cd6','1','1','0','<as><a><i><![CDATA[B]]></i></a></as>','25','','1','25','2011-04-21 21:18:49','1','10','1','0','0')" --number-of-queries=10000 -uspace -h192.168.56.221 -p888888
mysqlslap  --concurrency=10 --iterations=1 --create-schema=tongchao --query="INSERT INTO ol_answerresult_test (UniqueKey, TagCatalogId,RootCatalogId,UserId,QuestionId,CorrectCount,ErrorCount, LastIsCorrect,LastAnswerXML,TotalCostTime,Reviews, AnswerResultCategory, LastCostTime, LastAnswerTime,IsPublic,SUBJECT,TotalCount,AnswerMode,ExerciseWeight)VALUES('3313_67bd0665-3d82-4622-9bf0-c4bb36fd2cd6', 7122, '7115', 3313, '67bd0665-3d82-4622-9bf0-c4bb36fd2cd6','1','1','0','<as><a><i><![CDATA[B]]></i></a></as>','25','','1','25','2011-04-21 21:18:49','1','10','1','0','0')" --number-of-queries=10000 -uspace -h192.168.56.221 -p888888 -P3307
posted @ 2011-05-06 22:33 大柳树 阅读(148) 评论(0) 编辑

C:\Program Files\MySQL\MySQL Server 5.1\scripts>mysqlslap --defaults-file="C:/Pr
ogram Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=50 --iterations=1 --num
ber-int-cols=4 --auto-generate-sql --auto-generate-sql-load-type=update --engine
=myisam --number-of-queries=10000 -uroot -p8319999
update
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 16.187 seconds
        Minimum number of seconds to run all queries: 16.187 seconds
        Maximum number of seconds to run all queries: 16.187 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
read
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 5.703 seconds
        Minimum number of seconds to run all queries: 5.703 seconds
        Maximum number of seconds to run all queries: 5.703 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
write
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 1.765 seconds
        Minimum number of seconds to run all queries: 1.765 seconds
        Maximum number of seconds to run all queries: 1.765 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 7.968 seconds
        Minimum number of seconds to run all queries: 7.968 seconds
        Maximum number of seconds to run all queries: 7.968 seconds
        Number of clients running queries: 50
        Average number of queries per client: 1000
mixed:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 52.281 seconds
        Minimum number of seconds to run all queries: 52.281 seconds
        Maximum number of seconds to run all queries: 52.281 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 51.985 seconds
        Minimum number of seconds to run all queries: 51.985 seconds
        Maximum number of seconds to run all queries: 51.985 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
C:\Program Files\MySQL\MySQL Server 5.1\scripts>mysqlslap --defaults-file="C:/Pr
ogram Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=50 --iterations=1 --cre
ate-schema=edu_591up --query="select userid from ol_user where username='zbwangc
hao@163.com'" --number-of-queries=10000 -uroot -p8319999
Benchmark
        Average number of seconds to run all queries: 3.813 seconds
        Minimum number of seconds to run all queries: 3.813 seconds
        Maximum number of seconds to run all queries: 3.813 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
C:\Program Files\MySQL\MySQL Server 5.1\scripts>mysqlslap --defaults-file="C:/Pr
ogram Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=50 --iterations=10 --cr
eate-schema=edu_591up --query="select userid,username from ol_user where userid=
25091" --number-of-queries=10000 -uroot -p8319999
Benchmark
        Average number of seconds to run all queries: 1.746 seconds
        Minimum number of seconds to run all queries: 0.781 seconds
        Maximum number of seconds to run all queries: 1.954 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
C:\Program Files\MySQL\MySQL Server 5.1\scripts>mysqlslap --defaults-file="C:/Pr
ogram Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=50 --iterations=10 --cr
eate-schema=edu_591up --query="update ol_user set username='zbwangchao@163.com'
where userid=25091" --number-of-queries=10000 -uroot -p8319999
Benchmark
        Average number of seconds to run all queries: 2.229 seconds
        Minimum number of seconds to run all queries: 1.969 seconds
        Maximum number of seconds to run all queries: 3.172 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
C:\Program Files\MySQL\MySQL Server 5.1\scripts>mysqlslap --defaults-file="C:/Pr
ogram Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=50 --iterations=10 --cr
eate-schema=edu_591up --query="insert into ol_user(username,password)values('ton
gchao','dddddddddddddddd')" --number-of-queries=10000 -uroot -p8319999
Benchmark
        Average number of seconds to run all queries: 8.992 seconds
        Minimum number of seconds to run all queries: 8.078 seconds
        Maximum number of seconds to run all queries: 9.750 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
C:\Program Files\MySQL\MySQL Server 5.1\scripts>mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=50 --iterations=1 --create-schema=edu_591up --query="insert into ol_user(username,password)values('tongchao','dddddddddddddddd')" --number-of-queries=10000 -uroot -p8319999
Benchmark
        Average number of seconds to run all queries: 2.000 seconds
        Minimum number of seconds to run all queries: 2.000 seconds
        Maximum number of seconds to run all queries: 2.000 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
(无索引)
mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=50 --iterations=1 --create-schema=edu_591up --query="insert into ol_user(username,password)values('tongchao','dddddddddddddddd')" --number-of-queries=10000 -uroot -p8319999
mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=50 --iterations=1 --create-schema=edu_591up --query="update ol_user set password='tongchaotongchao' where userid = 13" --number-of-queries=10000 -uroot -p8319999
mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=50 --iterations=1 --create-schema=edu_591up --query="select * from ol_user where userid = 13" --number-of-queries=10000 -uroot -p8319999
*******************************************************************************************************************************
mysqlslap --defaults-file="D:/MySQL/MySql5.5/my.ini" --concurrency=50 --iterations=1 --create-schema=edu_591up --query="insert into ol_user(username,password)values('tongchao','dddddddddddddddd')" --number-of-queries=10000 -uroot
mysqlslap --defaults-file="D:/MySQL/MySql5.5/my.ini" --concurrency=50 --iterations=1 --create-schema=edu_591up --query="update ol_user set password='tongchaotongchao' where userid = 13" --number-of-queries=100000 -uroot
mysqlslap --defaults-file="D:/MySQL/MySql5.5/my.ini" --concurrency=50 --iterations=1 --create-schema=edu_591up --query="select * from ol_user where userid = 13" --number-of-queries=10000 -uroot
二、自动测试
mysqlslap --defaults-file="C:/Program Files/MySQL/MySQL Server 5.1/my.ini" --concurrency=50 --iterations=1 --number-int-cols=4 --auto-generate-sql --auto-generate-sql-load-type=update --engine=myisam,innodb --number-of-queries=10000 -uroot -p8319999
mysqlslap --defaults-file="D:/MySQL/MySql5.5/my.ini" --concurrency=50 --iterations=1 --number-int-cols=4 --auto-generate-sql --auto-generate-sql-load-type=update --engine=myisam,innodb --number-of-queries=10000 -uroot
mysqlslap --defaults-file="C:/Program Files/MySQL/mysql-5.1.54-win32/my.ini" --concurrency=50 --iterations=1 --number-int-cols=4 --auto-generate-sql --auto-generate-sql-load-type=update --engine=myisam,innodb --number-of-queries=10000 -uroot
结果:
5.1
update:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 17.547 seconds
        Minimum number of seconds to run all queries: 17.547 seconds
        Maximum number of seconds to run all queries: 17.547 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 47.860 seconds
        Minimum number of seconds to run all queries: 47.860 seconds
        Maximum number of seconds to run all queries: 47.860 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
write:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 1.587 seconds
        Minimum number of seconds to run all queries: 0.766 seconds
        Maximum number of seconds to run all queries: 1.890 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 1.868 seconds
        Minimum number of seconds to run all queries: 1.812 seconds
        Maximum number of seconds to run all queries: 2.094 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
mixed:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 52.453 seconds
        Minimum number of seconds to run all queries: 52.453 seconds
        Maximum number of seconds to run all queries: 52.453 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 66.531 seconds
        Minimum number of seconds to run all queries: 66.531 seconds
        Maximum number of seconds to run all queries: 66.531 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
5.5
update:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 14.360 seconds
        Minimum number of seconds to run all queries: 14.360 seconds
        Maximum number of seconds to run all queries: 14.360 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 43.843 seconds
        Minimum number of seconds to run all queries: 43.843 seconds
        Maximum number of seconds to run all queries: 43.843 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
        
write:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 1.526 seconds
        Minimum number of seconds to run all queries: 0.703 seconds
        Maximum number of seconds to run all queries: 1.766 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 1.573 seconds
        Minimum number of seconds to run all queries: 0.766 seconds
        Maximum number of seconds to run all queries: 1.828 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
mixed:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 39.468 seconds
        Minimum number of seconds to run all queries: 39.468 seconds
        Maximum number of seconds to run all queries: 39.468 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 50.468 seconds
        Minimum number of seconds to run all queries: 50.468 seconds
        Maximum number of seconds to run all queries: 50.468 seconds
        Number of clients running queries: 50
        Average number of queries per client: 200
C:\Documents and Settings\Administrator>mysqlslap --defaults-file="D:/MySQL/MySq
l5.5/my.ini" --concurrency=100 --iterations=5 --number-int-cols=4 --auto-generat
e-sql --auto-generate-sql-load-type=update --engine=innodb --number-of-queries=1
000 -uroot
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 3.371 seconds
        Minimum number of seconds to run all queries: 2.547 seconds
        Maximum number of seconds to run all queries: 4.515 seconds
        Number of clients running queries: 100
        Average number of queries per client: 10
        
        
 C:\Documents and Settings\Administrator>mysqlslap --defaults-file="C:/Program
les/MySQL/mysql-5.1.54-win32/my.ini" --concurrency=100 --iterations=5 --number
nt-cols=4 --auto-generate-sql --auto-generate-sql-load-type=update --engine=in
db --number-of-queries=1000 -uroot
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 3.831 seconds
        Minimum number of seconds to run all queries: 3.797 seconds
        Maximum number of seconds to run all queries: 3.860 seconds
        Number of clients running queries: 100
        Average number of queries per client: 10       
posted @ 2011-05-06 22:33 大柳树 阅读(255) 评论(0) 编辑

mysqlbinlog -R -h 192.168.56.221 -u tongchao -p111111 mysql0-bin.000001 --start-datetime="2010-12-22 00:00:00" --stop-datetime="2010-12-22 05:00:00"
远程binlog获取
需要权限,
replication slave权限

 

mysqlbinlog -d 591up /data0/mysql/3306/tc/log-bin.000536 --start-datetime='2010-12-20 00:02:50' | grep ol_question >/data0/mysql/ol_question2.txt
 mysqlbinlog -d 591up /data0/mysql/3306/tc/log-bin.000536 --start-datetime='2010-12-20 00:02:50' | grep ol_question >/data0/mysql/ol_question2.txt
mysqlbinlog -d servant_591up "D:/3.7/logbin/log-bin.000456" --start-datetime="2011-03-04 21:00:00"  --stop-datetime="2011-03-04 22:10:00" >d:/sql/2011race/binlog.sql
mysqlbinlog -d servant_591up D:\sql\0415race\log-bin.000545 >> D:\sql\0415race\servant_591up.sql
mysqlbinlog -d servant_591up D:\sql\0415race\log-bin.000546 >> D:\sql\0415race\servant_591up.sql
mysqlbinlog -d servant_591up D:\sql\0415race\log-bin.000547 >> D:\sql\0415race\servant_591up.sql
mysqlbinlog -d servant_591up D:\sql\0415race\log-bin.000548 >> D:\sql\0415race\servant_591up.sql
mysqlsla.pl -lt binary "D:\sql\0415race\servant_591up.sql" > D:\sql\0415race\mysqlsla.txt --top 100
mysqlsla.pl --log-type slow "D:/861028/slow_query.log" >d:/sql/slow.txt

mysqlsla.pl -lt slow "D:/_861028/slow_query.log" -R print-unique -mf "db=591up" -sf "+SELECT"  

 

posted @ 2011-05-06 22:32 大柳树 阅读(247) 评论(0) 编辑

MySQL与MSSQL 有一个区别在于MySQL建表的时候需要选择存储引擎,常用的存储引擎有MyISAM和InnoDB
而由于InnoDB是一个健壮的事务型存储引擎,已经有10多年的历史,一些重量级的互联网公司(Yahoo,Google Netease ,Taobao)也经常使用
我的日常工作也经常接触InnoDB,现在就InnoDB一部分可以改善性能的参数列举
1. innodb_additional_mem_pool_size 
除了缓存表数据和索引外,可以为操作所需的其他内部项分配缓存来提升InnoDB的性能。这些内存就可以通过此参数来分配。推荐此参数至少设置为2MB,实际上,是需要根据项目的InnoDB表的数目相应地增加
2.innodb_data_pool_size
此参数类似于MySQL的key_buffer参数,但特定用于InnoDB表.这个参数确定了要预留多少内存来缓存表数据和索引。与key_buffer一样,更高的设置会提升性能,可以是服务器的内存70-80%
3.innodb_data_file_path
参数的名字和实际的用途有点出入,它不仅指定了所有InnoDB数据文件的路径,还指定了初始大小分配,最大分配以及超出起始分配界线时是否应当增加文件的大小。此参数的一般格式如下:
path-to-datafile:size-allocation[:autoextend[:max-size-allocation]]
例如,假设希望创建一个数据文件sales,初始大小为100MB,并希望在每次达到当前大小限制时,自动增加8MB(8MB是指定autoextend时的默认扩展大小).但是,不希望此文件超过1GB,可以使用如下配置:
innodb_data_home_dir = 
innodb_data_file_path = /data/sales:100M:autoextend:8M: max:1GB
如果此文件增加到预定的1G的限制,可以再增加另外一个数据文件,如下:
innodb_data_file_path = /data/sales:100M:autoextend:8M: max:1GB;innodb_data_file_path = /data2/sales2:100M:autoextend:8M: max:2GB
要注意的是,在这些示例中,inndb_data_home_dir参数开始设置为空,因为最终数据文件位于单独的位置(/data/和/data2/).如果希望所有 InnoDB数据文件都位于相同的位置,就可以使用innodb_data_home_dir来指定共同位置,然后在通过 inndo_data_file_path来指定文件名即可。如果没有定义这些值,将在datadir中创建一个sales。
4 innodb_data_home_dir
此参数指定创建InnoDB表空间的路径的公共部分,默认情况下,这是MySQL的默认数据,由MySQL参数datadir指定
5. innodb_file_io_threads
此参数指定InnoDB表可用的文件I/O线程数,MySQL开发人员建议在非Windows平台中这个参数设置为4
6. innodb_flush_log_at_trx_commit
如果将此参数设置为1,将在每次提交事务后将日志写入磁盘。为提供性能,可以设置为0或2,但要承担在发生故障时丢失数据的风险。设置为0表示事务日志写入日志文件,而日志文件每秒刷新到磁盘一次。设置为2表示事务日志将在提交时写入日志,但日志文件每次刷新到磁盘一次。
7.innodb_log_archive
因为MySQL目前使用自己的日志文件恢复InnoDB表,此参数可设置为0
8.innodb_log_arch_dir
MySQL目前忽略此参数,但会在未来的版本中使用。目前,应当将其设置为与innodb_log_group_home_dir相同的值
9.innodb_log_buffer_size
此参数确定些日志文件所用的内存大小,以M为单位。缓冲区更大能提高性能,但意外的故障将会丢失数据.MySQL开发人员建议设置为1-8M之间
10. innodb_log_file_size
此参数确定数据日志文件的大小,以M为单位,更大的设置可以提高性能,但也会增加恢复故障数据库所需的时间
11.innodb_log_files_in_group
为提高性能,MySQL可以以循环方式将日志文件写到多个文件。推荐设置为3M
12. innodb_log_group_home_dir
此参数确定日志文件组中的文件的位置,日志组中文件的个数由innodb_log_files_in_group确定,此位置设置默认为MySQL的datadir
13.innodb_lock_wait_timeout
InnoDB 有其内置的死锁检测机制,能导致未完成的事务回滚。但是,如果结合InnoDB使用MyISAM的lock tables 语句或第三方事务引擎,则InnoDB无法识别死锁。为消除这种可能性,可以将innodb_lock_wait_timeout设置为一个整数值,指示 MySQL在允许其他事务修改那些最终受事务回滚的数据之前要等待多长时间(秒数)
14.skip-innodb
启用此参数能防止夹杂InnoDB表驱动程序,不使用InnoDB表时推荐此设置
posted @ 2011-05-06 22:21 大柳树 阅读(109) 评论(0) 编辑

SET @rowNum = 0;
SELECT userid,@rowNum:=@rowNum+1 AS rowNo FROM ol_user ORDER BY userid DESC LIMIT 10;
posted @ 2011-05-06 22:19 大柳树 阅读(22) 评论(0) 编辑

方法一:
select `name` from mysql.proc where db = 'your_db_name' and `type` = 'PROCEDURE'   //存储过程
 select `name` from mysql.proc where db = 'your_db_name' and `type` = 'FUNCTION'   //函数
方法二:
show procedure status; //存储过程

 

 SHOW PROCEDURE STATUS WHERE db='servant_591up' 

 show function status;     //函数

 

查看存储过程或函数的创建代码
show create procedure proc_name;
show create function func_name;
查看视图
SELECT * from information_schema.VIEWS   //视图
SELECT * from information_schema.TABLES   //表
查看触发器
方法一:
语法:SHOW TRIGGERS [FROM db_name] [LIKE expr]
实例:SHOW TRIGGERS\G     //触发器
方法二:
对INFORMATION_SCHEMA数据库中的TRIGGERS表查询
mysql>SELECT * FROM triggers T WHERE trigger_name=”mytrigger” \G
posted @ 2011-05-06 22:17 大柳树 阅读(94) 评论(0) 编辑

describe命令
一、describe命令用于查看特定表的详细设计信息,例如为了查看guestbook表的设计信息,可用:
describe guestbook

describe ol_user userid 

二、可通过”show comnus”来查看数据库中表的列名,有两种使用方式:
show columns form 表名 from 数据库名
或者:
show columns from 数据库名.表名

三、用describe命令查询具体列的信息
describe guestbook id
就是查询guestbook中id字段的列信息

{DESCRIBE | DESC} tbl_name [col_name | wild]
DESCRIBE 是 SHOW COLUMNS FROM 的缩写。

DESCRIBE 提供有关一个表的列信息。col_name 可以是一个列名或是一个包含 SQL 通配符字符 “%” 和 “_” 的字符串。没有必要用引号包围字符串。 

 

mysql> desc ol_user username\G
*************************** 1. row ***************************
  Field: UserName
   Type: varchar(100)
   Null: NO
    Key: MUL
Default:
  Extra:

1 row in set (0.02 sec) 

posted @ 2011-05-06 22:14 大柳树 阅读(630) 评论(0) 编辑

查看索引
mysql> show index from tblname;
mysql> show keys from tblname;
· Table
表的名称。
· Non_unique
如果索引不能包括重复词,则为0。如果可以,则为1。
· Key_name
索引的名称。
· Seq_in_index
索引中的列序列号,从1开始。
· Column_name
列名称。
· Collation
列以什么方式存储在索引中。在MySQL中,有值‘A’(升序)或NULL(无分类)。
· Cardinality
索引中唯一值的数目的估计值。通过运行ANALYZE TABLE或myisamchk -a可以更新。基数根据被存储为整数的统计数据来计数,所以即使对于小型表,该值也没有必要是精确的。基数越大,当进行联合时,MySQL使用该索引的机 会就越大。
· Sub_part
如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为NULL。
· Packed
指示关键字如何被压缩。如果没有被压缩,则为NULL。
· Null
如果列含有NULL,则含有YES。如果没有,则该列含有NO。
· Index_type
用过的索引方法(BTREE, FULLTEXT, HASH, RTREE)。
· Comment
posted @ 2011-05-06 22:08 大柳树 阅读(1161) 评论(0) 编辑

第一章
myisam,可以基于blob和text的前500字节,创建索引
myisam 支持fulltext
延迟更新索引
(delay_key_write)
CREATE TABLE `table3` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(30) DEFAULT NULL,
  `id2` INT(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
)  ENGINE=MYISAM DEFAULT CHARSET=utf8  DELAY_KEY_WRITE = 1
ALTER TABLE table2 DELAY_KEY_WRITE = 1
只有myisam支持全文检索
第三章 索引方面
  字段尽可能的小
  尽量避免null,用0代替。但是对性能的提升很小,最后考虑,索引的列最好不适用null
  
mysql性能优化点记录
一、优化数据访问
查询性能低下的最基本原因就是访问了太多数据。一些查询不可避免的要筛选大量的数据,单这并不常见。大部分性能欠佳的查询都可以用减
少数据访问的方式进行修改。在分析性能欠佳的查询的时候,下面两个步骤比较有用:
1.应用程序是否在获取超过需要的数据。这通常是访问了过多的行或列。
2.mysql服务器是否分析了超过需要的行。
对于访问的数据行很大,而生成的结果中数据行很少,可以尝试修改。

 

1.使用覆盖索引,它存储了数据,所以存储引擎不会去完整的行。
2.更改架构,一个例子就是使用汇总表。
3.重写复杂的查询,让mysql的优化器可以优化的执行。

 

二、复杂查询和多个查询
1.把一个复杂的查询分解为多个简单的查询。(mysql一般的服务器,每秒钟可以处理50 000个查询)
2.
三、缩短查询
将一次处理大量数据的操作,分解为多个小操作。循环的方式每次处理一部分数据。一次删除不要超过10 000行(delete)
四、分解链接
把一个多表连接分解成多个单个查询,然后在应用程序里实现联接。
这样的优势
1.缓存效率高。
2.mysql,可以更有效的利用表锁,查询会锁住单个表较短时间。
3.应用程序进行联接可以更方便的拓展数据库,把不同表放在不同服务器上。
4.查询更高效。
5.可以减少多余的行访问,可以减少网络流量和内存消耗。
 小结:在程序端进行联接的效率更高
 1.可以缓存早期查询的大量数据。
 2.使用了多个myisam表
 3.数据分布在不同的服务器上。
 4.对于大表使用in替换联接
 5.一个连接引用了同一个表多次。
 
 当你重建汇总和缓存表的时候,在操作的时候你常常需要它们的数据保持可见。你可以使用“shadow table”(影像表)来实现。当你已经创建它之后,你可以使用原子性的重命名来交换这些表。举个例子,如果
你需要重建my_summary,你能创建my_summary_new,填充数据,把它和真正的表作交换。
mysql> DROP TABLE IF EXISTS my_summary_new, my_summary_old;
mysql> CREATE TABLE my_summary_new LIKE my_summary;
-- populate my_summary_new as desired
mysql> RENAME TABLE my_summary TO my_summary_old, my_summary_new TO my_summary;
 
 mysql执行查询的一般性过程
 1.客户端发送查询到服务器
 2.服务器检查查询缓存,
 3.服务器解析,预处理和优化查询,生成执行计划。
 4.执行引擎调用存储引擎api执行查询。
 5.服务器将结果发送到客户端。
 
 mysql客户端、服务器协议
 1.协议是半双工的。mysql服务器在某个时间可以发送或者接受数据,单不能同时发送和接收。所有没有办法阶段消息。
 
 2.客户端用一个数据包将查询发送到服务器,所以max_packet_size这个配置参数对于大查询很重要的原因。
 3.客户端从服务器提取数据的时候是服务器产生数据的同时把它们“推”到客户端的,客户端只需要接收推出来的数据,无法告诉服务器停止
发送数据。
 
 查询缓存

 

 SELECT SQL_NO_CACHE * FROM ol_answerlog LIMIT 1000
 SHOW STATUS LIKE 'last_query_cost'
 
 
 关键字straight_join 强制执行引擎按照查询中表现的顺序来进行链接操作。
严格的说,mysql不回尝试减少读取的行数,它只会试着优化对页面的读取,但是行数可以大致显示查询的开销。
 连接优化器试着产生最低开销的查询计划。在可能的时候,他会从单表计划开始,检查所有的可能的子树的组合。但是对n个表连接,需要检
查组合的数量就是n的阶乘,这个数量称为ie搜索空间, 它增长非常快,如果一个查询需要连接10个表,那么要检查的数量将是10!=36288000 
当搜索空间非常巨大的时候优化耗费的时间就会非常长,这时候服务器就不回执行完整的分析,但表的数量超过optimizer_search_depth的值
时,它就会走捷径,比如执行所谓的 贪婪搜索。

 

SHOW TABLE STATUS FROM `servant_591up`WHERE ENGINE IS NOT NULL
AND NAME LIKE '%ol_ans%';

 

max min的优化
select min(id) from ol_user where username = 'dddd@dd.com'
(一)max 和 min 会扫描整张表。
mysql的主键都是按照升序排列的。
可以使用limit 改写查询,
select userid from ol_user where username='test@nd.com' limit 1
(二)对同一表进行select 和 update
mysql不允许对一个表进行update的时候进行select
update tb1 as out_table
set cnt = (select count(*) from tb1 as inner_table where inner_table.type = outer_table.type);
一个实现方式:衍生表,当成临时表来处理。
update tb1 inner join(
select type,count(*) as cnt
from tb1
group by type
)as der using(type)
set tb1.cnt = der.cnt;
(三、)优化特定类型的查询
1.count
count的作用 统计值的数量和统计行的数量
值是非空表达式(NOT NULL)
一个常见的错误就是在想统计行数的时候,在count的括号中放入列名,如果想知道结果的行数,应该总是使用COUNT(*),这可以清晰的说明意
图,并且得到好的性能。
2.MYISAM
只有在没有WHERE条件的时候COUNT(*)才是最快的,在有条件过滤的时候并不非常快。
3.简单优化
可以利用MYISAM对COUNT(*)的优化对已经有索引的一小部分做统计。
SELECT COUNT(*) FROM WORD.CITY WHERE ID>5;
优化为下面的语句
SELECT (SELECT COUNT(*) FROM CITY) - COUNT(*) FROM CITY WHEREID<=5;
这样的explain只扫描6行数据
使用一个查询统计同一列中不同值的数量。
select sum(if(color='blue',1,0)) as blue,sum(if(color='red',1,0)) as red from items;
下面是一个等价查询
select count(color='blue' or null) as blue,count(color='red' or null) as red from items;
(四)优化联接
1.确保on 或using使用的列上有索引。
通常只需要在联接中的第2个表上添加索引就可以。
2.确保group by或order by只引用一个表中的列。这样可以使用索引。
3.谨慎升级mysql
(五)优化子查询
对于子查询,尽可能的使用联接。
(五)优化group by和distinct
1.主要方式:索引
2.优化group by的策略:临时表或文件排序分组。
SQL_SMALL_RESULT : 强制使用临时表
SQL_BIG_RESULT :强制使用文件排序
通常对表的id进行分组会更加高效
可以使用SQL_MODE参数禁止SELECT中使用在group by中出现的列
子查询创建的临时表不支持索引。
所以要让子查询创建的临时表尽可能的小。
3.使用ROLL UP 优化GROUP BY
WITH ROLLUP
最好的方式是将WITH ROLLUP 放在应用程序里。
注意: Rollup 与 order by 相互排拆 
(六)优化limit和offset
LIMIT 和ORDER BY 一块使用。
如果没有索引,就使用文件排序。
(七)优化SQL_CALC_FOUND_ROWS
这个地方很重要
一个技巧:在含有limit的查询中添加SQL_CALC_FOUND_ROWS,这样就可以知道没有limit的时候会返回多少行数据。服务器会预测将会发现多少
行数据。但是服务器并不能真正的做到,只是告诉服务器生成结果并丢掉结果中不需要的部分。而不是在得到需要的数据后就立即停止。这个
选项代价很高。
一个非常好的设计:
如果每页有20条结果,那么应该查询limit 21行数据,只显示20条,如果结果中有21行,那么就会有下一页。
另一种方式:就是提取并缓存大量数据,比如1000行,然后从缓存中获取后续页面的数据。
可以让程序知道一共有多少数据,少于1000,程序知道有多少页,如果大于1000,可以显示找到的结果超过1000个。
这两种都比重复产生完整的结果效率高。
如果以上两种都不可以使用,可以使用覆盖索引,使用单独的count(*)会更好
(八)优化联合 union
mysql总是使用临时表来执行union,无法做更多的优化
重要的是,一定要使用union all,除非真的是需要服务器消除重复的行,
否则mysql会使用distinct选项,来确保所有行数据的唯一性。
(九)查询优化提示
可以用一些提示控制优化器的行为,每个提示只影响当前查询。
1.HIGH_PRIORITY  和 LOW_PRIORITY
HIGH_PRIORITY 让mysql 将一个select语句放在其他的语句的前面,mysql将它放在队列的前面,而不是在队列中等待。可以用在insert语句中
low——priority正好相反,可以用在SELECT INSERT UPDATE REPLACE DELETE 
这两个选项在表锁的存储过程中有效,在innerdb无效,在myisam要小心使用,严重影响性能,禁止并发插入。
2.delayed
用户insert delete
立即返回,放入缓冲当中,,无法使用LAST_ISNERT_ID()
3.STRAIGHT_JOIN
强制mysql按照查询中表出现的顺序来连接表。
出现在两个连接的表中间时,强制这两个表按照顺序连接。
用途:mysql没有选择更好的链接,或者优化器需要花费很长时间来确定连接顺序。
4.SQL_SMALL_RESULT SQL_BIGA_RESULT
用在group by 和distinct语句中的,如何使用临时表
SQL_SMALL_RESULT :结果很小,可以放在索引过的临时表中,
SQL_BIGA_RESULT:结果很大,最好使用磁盘上的临时表进行排序。
5.SQL_BUFFER_RESULT
将结果放在临时表中,并且要尽快释放掉表锁。
6.SQL_CACHE SQL_NO_CACHE
7.SQL_CALC_FOUND_ROWS
在limit自居计算完整的结果集,可以通过found_ROWS()来取得它的行数,
最好不使用这个提示、
8.FOR_ UPDATE  和   LOCK IN SHARE MODE
只有innodb支持,提示控制锁定,仅对行锁起作用。select语句中
9.USE INDEX 和 IGNORE INDEX  和  FORCE INDEX
控制索引的使用,在mysql5.1中,还有 FOR ORDER BY  FOR GROUP BY 
用途:告诉优化器表扫描比索引代价高很多,
重要的系统变量
Optimizer_search_depth
优化器检查执行计划的深度。
Optimizer_prune_level
根据检查的行数来决定跳过一些查询计划。
(九) 用户自定义变量
一些需要注意的问题:
会禁止缓存
不能用于文字常量和标识的地方(表名,列名,limit)
和连接有关,不能跨通信使用
如果使用连接池,会引起代码隔离
mysql 5.0大小写敏感
不能显示的声明类型,最好的方式给变量显示的一个初始值 0   0.0  ‘’,
用户自定义变量的类型是动态的,赋值的时候才会变化。
优化器有时候会把变量优化掉。
set @ont:=1; :=运算符的优先级,低于其他的,最好使用括号()。。 也可以使用=赋值,最好统一使用:=
未定义的变量不会引起语法错误,很容易犯错。
五、mysql高级特性
查询缓存命中率
Qcache_hits/(Qcache_his+Com_select)   show status
1.缓存未命中原因
查询不可缓存,不确定函数。CURRENT_DATE,结果太大, Qcache_not_cached 会记录两种无法缓存的查询数量。
服务器以前从来没见过这个缓存。
查询结果被缓存过,但是服务器把它移除。
很多缓存失效。
2.很多缓存未命中,但是不能缓存的查询很少。
查询缓存未被激活。
服务器看到了以前未见过的查询
缓存失效。
A:如何对查询缓存进行维护调优
Query_cache_type
表示缓存是否被激活,on off demand,demand:只有sql_cache的查询才可以被缓存。
Query_cache_size
缓存的总内存,字节单位。1024的倍数,
Query_cache_min_res_unit
分配缓存块的最小值
Query_cache_limit
限制了mysql存储的最大结果。如果超过这个值,会丢掉已经缓存过的值,并增加Query_not_chched的值。如果是这样需要在查询上增
加QUERY_NO_CACHE
Query_cache_wlock_invalidate
是否缓存其他链接已经锁定了的表,默认off,
B:优化的一些法则
减少碎片
需要仔细选择Query_cache_min_res_unit,可以避免在查询缓存中造成大量的内存浪费。
最佳设置根据典型查询结果确定。可以使用内存(Queryc_cache_size - Query_free_memory)除以Query_queries_in_cache
得到查 询的平均大小。可以通过query_cache_limit的值来阻止缓存大结果。
可以检查Qcache_free_blocks来探测缓存中碎片的情况,显示缓存中有多少内存块出于free状态。如果Qcache_free_blocks
大致 等于Qcache_total_blocks/2,则说明碎片非常严重,如果Qcache_lowmen_prunes的值在增加并且有大量的
自由块,说明碎片导致查 询整 被从缓存中永久删除。
可以使用FLUSH QUERY CACHE命令移除碎片。会把所有的存储块向上移动,把自由块移动到底部。会阻止访问查询缓存,锁定
整个服务器,通常这个速度很快,不会移除缓存中的数据,
RESET QUERY CACHE会清除缓存数据。
提高缓存可用性。
如果没有碎片,但是命中率不高,应该给缓存分配较少的内存。
服务器清理查询的时候会Qcache_lowmen_prunes的值会增加,如果值增加的过快则说明:
1.如果自由块很多,碎片
2.如果自由块比较少,说明工作负载使用的内存超过所分配的内存。可以检查Qcache_free_memory知道未使用的内
存。
可以禁用缓存查看缓存是否真的有效果
query_cacha_size = 0,可以关闭缓存(query_cache_type 无法影响已经打开了的链接,也不会把内存归还给服务
器。)
5.1.5 innodb和查询缓存
6.2.2 myisam键缓存
SHOW VARIABLES LIKE 'key_buffer_size'
键缓冲区,myisam本身只缓存索引,没有数据。
应该让key_buffer_size占到保留内存的25-50%。但是对于mysql5.0,最大上限都是4G
但是可以创建多个命名键缓冲区。可以一次在内存中保存4G以上的数据。
key_buffer_1  key_buffer_2 都是1G
在配置文件增加两行 
key_buffer_1.key_buffer_size = 1G
key_buffer_2.key_buffer_size = 1G
可以使用cache index 命令把表映射到缓存
也可以用下面的命令把表的索引保存到key_buffer_1
cache index t1,t2 in key_buffer_1
也可以使用load index把表的索引预加载到缓存中
load index into cache t1,t2;
缓存命中率
100-((Key_reads * 100) / Key_read_requests)
缓存使用百分比
100-((Key_blocks_unused * key_cache_block_size) * 100 / key_buffers_size)
innodb 可以使用裸设备

raw

posted @ 2011-05-06 21:58 大柳树 阅读(650) 评论(0) 编辑

INSERT语法
INSERT [LOW_PRIORITY |DELAYED| HIGH_PRIORITY] [IGNORE]
       [INTO]tbl_name[(col_name,...)]
       VALUES ({expr| DEFAULT},...),(...),...
       [ON DUPLICATE KEY UPDATEcol_name=expr, ... ]
或:
INSERT [LOW_PRIORITY |DELAYED| HIGH_PRIORITY] [IGNORE]
       [INTO]tbl_name
       SETcol_name={expr| DEFAULT}, ...
       [ON DUPLICATE KEY UPDATEcol_name=expr, ... ]
或:
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
       [INTO]tbl_name[(col_name,...)]
       SELECT ...
       [ON DUPLICATE KEY UPDATEcol_name=expr, ... ]
 
一、DELAYED的使用

 

使用延迟插入操作
DELAYED调节符应用于INSERT和REPLACE语句。当DELAYED插入操作到达的时候,服务器把数据行放入一个队列中,并立即给客户端返回一个状态信息,这样客户端就可以在数据表被真正地插入记录之前继续进行操作了。如果读取者从该数据表中读取数据,队列中的数据就会被保持着,直到没有读取者为止。接着服务器开始插入延迟数据行(delayed-row)队列中的数据行。在插入操作的同时,服务器还要检查是否有新的读取请求到达和等待。如果有,延迟数据行队列就被挂起,允许读取者继续操作。当没有读取者的时候,服务器再次开始插入延迟的数据行。这个过程一直进行,直到队列空了为止。
几点要注意事项:· INSERT DELAYED应该仅用于指定值清单的INSERT语句。服务器忽略用于INSERT DELAYED...SELECT语句的DELAYED。· 服务器忽略用于INSERT DELAYED...ON DUPLICATE UPDATE语句的DELAYED。· 因为在行被插入前,语句立刻返回,所以您不能使用LAST_INSERT_ID()来获取AUTO_INCREMENT值。AUTO_INCREMENT值可能由语句生成。· 对于SELECT语句,DELAYED行不可见,直到这些行确实被插入了为止。· DELAYED在从属复制服务器中被忽略了,因为DELAYED不会在从属服务器中产生与主服务器不一样的数据。
注意,目前在队列中的各行只保存在存储器中,直到它们被插入到表中为止。这意味着,如果您强行中止了mysqld(例如,使用kill -9)或者如果mysqld意外停止,则所有没有被写入磁盘的行都会丢失。

 

二、IGNORE的使用

 

IGNORE是MySQL相对于标准SQL的扩展。如果在新表中有重复关键字,或者当STRICT模式启动后出现警告,则使用IGNORE控制ALTER TABLE的运行。如果没有指定IGNORE,当重复关键字错误发生时,复制操作被放弃,返回前一步骤。如果指定了IGNORE,则对于有重复关键字的行,只使用第一行,其它有冲突的行被删除。并且,对错误值进行修正,使之尽量接近正确值。
insert ignore into tb(...) value(...)
这样不用校验是否存在了,有则忽略,无则添加

 

三、ON DUPLICATE KEY UPDATE的使用

 

如果您指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则执行旧行UPDATE。例如,如果列a被定义为UNIQUE,并且包含值1,则以下两个语句具有相同的效果:mysql>INSERT INTO table (a,b,c) VALUES (1,2,3)
       ->ON DUPLICATE KEY UPDATE c=c+1;
 
mysql>UPDATE table SET c=c+1 WHERE a=1;
如果行作为新记录被插入,则受影响行的值为1;如果原有的记录被更新,则受影响行的值为2。
注释:如果列b也是唯一列,则INSERT与此UPDATE语句相当:
mysql>UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;
如果a=1 OR b=2与多个行向匹配,则只有一个行被更新。通常,您应该尽量避免对带有多个唯一关键字的表使用ON DUPLICATE KEY子句。
您可以在UPDATE子句中使用VALUES(col_name)函数从INSERT...UPDATE语句的INSERT部分引用列值。换句话说,如果没有发生重复关键字冲突,则UPDATE子句中的VALUES(col_name)可以引用被插入的col_name的值。本函数特别适用于多行插入。VALUES()函数只在INSERT...UPDATE语句中有意义,其它时候会返回NULL。
示例:
mysql>INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
       ->ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
本语句与以下两个语句作用相同:
mysql>INSERT INTO table (a,b,c) VALUES (1,2,3)
       ->ON DUPLICATE KEY UPDATE c=3;
mysql>INSERT INTO table (a,b,c) VALUES (4,5,6)
       ->ON DUPLICATE KEY UPDATE c=9;
当您使用ON DUPLICATE KEY UPDATE时,DELAYED选项被忽略。
 
总结:DELAYED 做为快速插入,并不是很关心失效性,提高插入性能。
        ignore     只关注主键对应记录是不存在,无则添加,有则忽略。
     ON DUPLICATE KEY UPDATE 在添加时操作,关注非主键列,注意与ignore的区别。有则更新指定列,无则添加。
 
posted @ 2011-05-06 21:54 大柳树 阅读(1136) 评论(0) 编辑

1.grants

GRANT REPLICATION SLAVE,FILE ON *.* TO 'repl1'@'192.168.56.221' IDENTIFIED
 BY '123456';
FLUSH PRIVILEGES;

GRANT REPLICATION SLAVE,FILE ON *.* TO 'repl2'@'192.168.56.77' IDENTIFIED
 BY '123456';
FLUSH PRIVILEGES;

2.
(56.77)
log-bin=mysql-bin #slave会基于此log-bin来做replication
server-id=1 #master的标示

binlog-do-db=tongchao
binlog-ignore-db=mysql
replicate-do-db=tongchao
replicate-ignore-db=mysql
log-slave-updates

slave-skip-errors=all

sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1


#user = mysql #未知
#log-bin=mysql-bin #在从服务器上启动日志记录,不是必须,但是官方建议
#server-id= 1 #服务器编号,唯一
#binlog-do-db=asteriskcdrdb #要同步的数据库
#binlog-ignore-db=mysql #不同步的数据?
#replicate-do-db=asteriskcdrdb #复制的数据库
#replicate-ignore-db=mysql #未知
#log-slave-updates #如果一个MASTER 挂掉的话,另外一个马上接管。
#slave-skip-errors=all #
#sync_binlog=1 #同步时钟
#auto_increment_increment=2 #
#auto_increment_offset=1 #自增涨增量



(56.221)

log-bin=mysql-bin
server-id= 2
binlog-do-db=tongchao
binlog-ignore-db=mysql
replicate-do-db=tongchao
replicate-ignore-db=mysql
log-slave-updates #如果一个MASTER 挂掉的话,另外一个马上接管。
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2

#sync_binlog=1
#auto_increment_increment=2
#auto_increment_offset=2


3.重启mysql
4.
进入MYSQL的SHELL。

flush tables with read lock
show master status

56.77
*************************** 1. row ***************************
            File: mysql-bin.000012
        Position: 106
    Binlog_Do_DB: tongchao
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)

56.221

*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 98
    Binlog_Do_DB: tongchao
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)

5.在各自机器上执行CHANGE MASTER TO命令
56.77
change master to
master_host='192.168.56.221',
master_user='repl2',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=98;
 start slave;

56.221

change master to
master_host='192.168.56.77',
master_user='repl1',
master_password='123456',
master_log_file='mysql-bin.000012',
master_log_pos=106;
 start slave;
6、查看各自机器上的IO进程和 SLAVE进程是否都开启。
show processlist\G
7、释放掉各自的锁,然后进行插数据测试。
unlock tables;


MYSQL 的 MASTER到MASTER的主主循环同步

环境:
A 192.168.104.2
B 192.168.104.3

A
grant replication slave,file on *.* to ‘repl1′@’192.168.104.3′ identified by ’123456′;
FLUSH PRIVILEGES ;

B
GRANT REPLICATION SLAVE , FILE ON * . * TO ‘repl2′@’192.168.104.2′ IDENTIFIED BY ’123456′;
FLUSH PRIVILEGES ;

关闭两台服务器的msyql服务
mysqladmin -u root -p shutdown

A:
user = mysql #未知
log-bin=mysql-bin #在从服务器上启动日志记录,不是必须,但是官方建议
server-id= 1 #服务器编号,唯一
binlog-do-db=asteriskcdrdb #要同步的数据库
binlog-ignore-db=mysql #不同步的数据?
replicate-do-db=asteriskcdrdb #复制的数据库
replicate-ignore-db=mysql #未知
log-slave-updates #如果一个MASTER 挂掉的话,另外一个马上接管。
slave-skip-errors=all #
sync_binlog=1 #同步时钟
auto_increment_increment=2 #
auto_increment_offset=1 #自增涨增量

B:
user = mysql
log-bin=mysql-bin
server-id= 2
binlog-do-db=asteriskcdrdb
binlog-ignore-db=mysql
replicate-do-db=asteriskcdrdb
replicate-ignore-db=mysql
log-slave-updates #如果一个MASTER 挂掉的话,另外一个马上接管。
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2

#sync_binlog=1
#auto_increment_increment=2
#auto_increment_offset=2
#服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。

重启这两台mysql server

然后执行/usr/bin/mysqld_safe & #把mysqld_safe放入后台执行 如果不能执行先stop mysqld

分别进入两台mysql 的shell控制台
A:
mysql> flush tables with read lock\G;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 98
Binlog_Do_DB: asteriskcdrdb,asteriskcdrdb
Binlog_Ignore_DB: mysql,mysql
1 row in set (0.00 sec)
B:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 98
Binlog_Do_DB: asteriskcdrdb,asteriskcdrdb
Binlog_Ignore_DB: mysql,mysql
1 row in set (0.00 sec)

然后保证两台数据库的记录一致,然后继续往下:
在各自机器上执行CHANGE MASTER TO命令。

A:
mysql> change master to
-> master_host=’192.168.104.3′,
-> master_user=’repl2′,
-> master_password=’123456′,
-> master_log_file=’mysql-bin.000002′, #同B服务器当中File: mysql-bin.000002
-> master_log_pos=98; #同B服务器录中Position: 98
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

B:

mysql> change master to
-> master_host=’192.168.104.2′,
-> master_user=’repl1′,
-> master_password=’123456′,
-> master_log_file=’mysql-bin.000002′, #同A服务器当中File: mysql-bin.000002
-> master_log_pos=98; #同B服务器录中Position: 98
Query OK, 0 rows affected (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

查看各自机器上的IO进程和 SLAVE进程是否都开启。
A:
mysql>show processlist\G
mysql> show processlist\G
*************************** 1. row ***************************
Id: 74
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 159
User: system user
Host:
db: NULL
Command: Connect
Time: 100
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 160
User: system user
Host:
db: NULL
Command: Connect
Time: 100
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 4. row ***************************
Id: 169
User: repl1
Host: 192.168.104.3:4240
db: NULL
Command: Binlog Dump
Time: 55
State: Has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
4 rows in set (0.00 sec)

B:
mysql>show processlist\G

mysql> show processlist\G
*************************** 1. row ***************************
Id: 4
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 7
User: repl2
Host: 152.104.141.19:58615
db: NULL
Command: Binlog Dump
Time: 51
State: Has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
*************************** 3. row ***************************
Id: 8
User: system user
Host:
db: NULL
Command: Connect
Time: 40
State: Waiting for master to send event
Info: NULL
*************************** 4. row ***************************
Id: 9
User: system user
Host:
db: NULL
Command: Connect
Time: 40
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
4 rows in set (0.01 sec)

如果出错误,查看/var/log/mysqld.log 当中的日志,注意权限、访问控制、master_log_file、master_log_pos 等值

释放掉各自的锁,然后进行插数据测试。
A:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
B:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

下面可以通过添加表,删除表,添加记录,删除记录等方法测试双master备份。

根据测试结果,失败再看日志,成功就大功告成
posted @ 2011-05-06 21:51 大柳树 阅读(59) 评论(0) 编辑