1. 程式人生 > >mysql開發之---每日一得01

mysql開發之---每日一得01

查看 delete div global rop 清空 rem not use

2015年7月7日-------------------------

1、truncate表會清空建表語句auto_increment的值;某個表的id即是主鍵也是自增,你能夠選擇插入隨意id值,假設不從1開始插入。從3開始insert,再插入沒有id的值時。自增值是4

2、查看每種引擎的索引大小。來優化數據庫參數
SELECT ?ENGINE, ?
ROUND(SUM(data_length) /1024/1024, 1) AS "Data MB", ?
ROUND(SUM(index_length)/1024/1024, 1) AS "Index MB", ?

ROUND(SUM(data_length + index_length)/1024/1024, 1) AS "Total MB", ?
COUNT(*) "Num Tables" ?
FROM ?INFORMATION_SCHEMA.TABLES ?
WHERE ?table_schema not in ("information_schema", "performance_schema") ?
GROUP BY ?ENGINE;?

3、使用prepare stmt from準備一個動態sql語句時。主要
(1)被準備的語句定義時必須是會話級的變量不能是local變量,須要加@進行定義。準備後的語句直到會話結束才會丟失。能夠使用deallocate prepare stmt消除分配的語句
表名不確定,檢查這個表最大id。從id+1開始插入10行數據
BEGIN
-- 在存儲過程中,一般的sql中values能夠是變量,可是表名、字段名不能是變量
declare v_xname varchar(20) default ‘testincre1‘;
delete from test.testincre1 where id=1;
select ifnull(max(id),0)+1 into @incre from test.testincre1;
set @end=@incre+10;
repeat
?set @sql=concat(‘insert into test.‘,v_xname,‘ values(@incre,‘‘yangsq‘‘,now());‘);
?select @sql;
?prepare stmt from @sql;
?execute stmt;
?deallocate prepare stmt;
set @incre=@incre+1;
until @incre=@end end repeat;
END
4、sql_slave_skip_counter
Last_SQL_Error: Error ‘Unknown table ‘sakila.testrepldb‘‘ on query. Default database: ‘sakila‘. Query: ‘DROP TABLE `testrepldb` /* generated by server */‘
mysql> start slave sql_thread; 報錯:會重復運行引起錯誤的sql,可是io_thread仍然正常會接受
2015-07-08 10:42:25 12378 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2015-07-08 10:42:25 12378 [Note] Slave SQL thread initialized, starting replication in log ‘mysql-bin.000012‘ at position 4449, relay log ‘./yaolansvr_slave01-relay-bin.000014‘ position: 283
2015-07-08 10:42:25 12378 [ERROR] Slave SQL: Error ‘Unknown table ‘sakila.testrepldb‘‘ on query. Default database: ‘sakila‘. Query: ‘DROP TABLE `testrepldb` /* generated by server */‘, Error_code: 1051
2015-07-08 10:42:25 12378 [Warning] Slave: Unknown table ‘sakila.testrepldb‘ Error_code: 1051
2015-07-08 10:42:25 12378 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log ‘mysql-bin.000012‘ position 4449

select @@sql_slave_skip_counter;
stop slave;--或者stop slave sql_thread
set global sql_slave_skip_counter=1;
start slave;

--log-error:
2015-07-08 10:53:30 12378 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2015-07-08 10:53:30 12378 [Note] Slave SQL thread initialized, starting replication in log ‘mysql-bin.000012‘ at position 4449, relay log ‘./yaolansvr_slave01-relay-bin.000014‘ position: 283
2015-07-08 10:53:30 12378 [Note] ‘SQL_SLAVE_SKIP_COUNTER=1‘ executed at relay_log_file=‘./yaolansvr_slave01-relay-bin.000014‘, relay_log_pos=‘283‘, master_log_name=‘mysql-bin.000012‘, master_log_pos=‘4449‘ and new position at relay_log_file=‘./yaolansvr_slave01-relay-bin.000014‘, relay_log_pos=‘410‘, master_log_name=‘mysql-bin.000012‘, master_log_pos=‘4576‘?

5、從 sqlserver 查詢mysql 報錯 從數據類型 dbtype_dbtimestamp 轉化為 datetime 時出錯
mysql某表datetime類型數據是0028-01-01 00:00:00。插入sqlserver datetime報錯,sqlserver datime支持的日期類型範圍是1753 年 1 月 1 日到 9999 年 12 月 31 日

6、replicate的相關參數比較
--replicate-do-table:沒有like pattern的功能,多個表須要指定多次
--replicate-wild-do-table:用like pattern的功能Example: --replicate-wild-do-table=foo%.bar% replicates only updates that use a table where the database name starts with foo and the table name starts with bar

mysql開發之---每日一得01