每當(dāng)跨月的時(shí)候也是系統(tǒng)出問題最多的時(shí)候,沒有表和字段缺失是兩個(gè)最常見的錯(cuò)誤。
為了解決這個(gè)問題,研究了一下MySQL的 information_schema 表:
information_schema這張數(shù)據(jù)表保存了MySQL服務(wù)器所有數(shù)據(jù)庫的信息。如數(shù)據(jù)庫名,數(shù)據(jù)庫的表,表欄的數(shù)據(jù)類型與訪問權(quán)限等。
再簡單點(diǎn),這臺(tái)MySQL服務(wù)器上,到底有哪些數(shù)據(jù)庫、各個(gè)數(shù)據(jù)庫有哪些表,每張表的字段類型是什么,各個(gè)數(shù)據(jù)庫要什么權(quán)限才能訪問,等等信息都保存在information_schema表里面。
OK!事情已經(jīng)變得相當(dāng)簡單了。高手可以無視以下內(nèi)容了。
按月分表為例:
????第一步:查詢出當(dāng)月以及下月滿足分表規(guī)則的表名
????????select table_name,table_schema from information_schema.tables where table_schema =@DatabaseName and table_type='base table' and table_name like @table_name;
????????當(dāng)然你可以拆分為兩步來實(shí)現(xiàn)。
????第二步:對比當(dāng)月與下月表名數(shù)據(jù)和創(chuàng)建表。
????????當(dāng)月有的下月沒有,這時(shí)候就應(yīng)該創(chuàng)建一個(gè)下月的表。
????????如何創(chuàng)建?最簡單也是最保險(xiǎn)的就是copy當(dāng)月的表。
為啥不用建表語句?平時(shí)升庫的時(shí)候,你去修改過建表語句?修改過建表事件?如果你修改過那最好!
????????Copy當(dāng)月表我們只需要表結(jié)構(gòu)不需要表數(shù)據(jù)所以一條sql語句就搞定了:
????????CREATE TABLE 下月表 LIKE 當(dāng)月表;
????這樣一來,兩條語句就搞定。
????好我們來聊聊代碼:
????三層架構(gòu) business Dal UI 。UI 和 DAL 沒啥說的。
????????直接上核心代碼:
????/// <summary>
/// 掃描并創(chuàng)建表
/// </summary>
PRivate void AnalyseAndCreatTables()
{
int scantime = 5;
while (true)
{
try
{
ShowBusiness.ShowMsg("獲取數(shù)據(jù)庫信息");
// 獲取數(shù)據(jù)庫信息
BGetDataBseName getdatabaseinfo = new BGetDataBseName();
getdatabaseinfo.Execute();
List<string> databases = getdatabaseinfo.DataBases;
if (databases != null)
{
foreach (var itemDatabase in databases)
{
this.HandSpiltByMonth(itemDatabase);
}
}
}
catch (Exception ex)
{
?
}
finally
{
Thread.Sleep(scantime * 1000 * 60);
}
}
}
?
?
/// <summary>
/// 處理按月分表
/// </summary>
/// <param name="databaseName">數(shù)據(jù)庫名</param>
private void HandSpiltByMonth(string databaseName)
{
ShowBusiness.ShowMsg(string.Format("處理【{0}】數(shù)據(jù)庫 按月分表", databaseName));
#region 按月分表
string nowtablePostfix = string.Format("{0}{1}", DateTime.Now.Year.ToString().Substring(2, 2), DateTime.Now.Month.ToString().PadLeft(2, '0'));
string nexttablePostfix = string.Format("{0}{1}", DateTime.Now.AddMonths(1).Year.ToString().Substring(2, 2), DateTime.Now.AddMonths(1).Month.ToString().PadLeft(2, '0'));
ShowBusiness.ShowMsg(string.Format("處理【{0}】數(shù)據(jù)庫中按月分表的當(dāng)前月的表", databaseName));
// 獲取該數(shù)據(jù)庫中按月分表的當(dāng)前月的表
DGetTablesFromDatabase getNowDatatables = new DGetTablesFromDatabase(databaseName, nowtablePostfix);
getNowDatatables.Execute();
List<string> nowtablse = getNowDatatables.TablesList;
ShowBusiness.ShowMsg(string.Format("處理【{0}】數(shù)據(jù)庫當(dāng)月的表:{1}", databaseName, string.Join(",", nowtablse.ToArray())));
?
// 獲取該數(shù)據(jù)庫中按月分表的下月的表
DGetTablesFromDatabase getNextDatatables = new DGetTablesFromDatabase(databaseName, nexttablePostfix);
getNextDatatables.Execute();
List<string> nextDatatables = getNextDatatables.TablesList;
ShowBusiness.ShowMsg(string.Format("處理【{0}】數(shù)據(jù)庫下月的表:{1}", databaseName, string.Join(",", nextDatatables.ToArray())));
// 對比當(dāng)前月表和下月表,如果下月表中沒有,則新建
if (nowtablse != null)
{
foreach (var nowitem in nowtablse)
{
ShowBusiness.ShowMsg(string.Format("處理【{0}】數(shù)據(jù)庫對比當(dāng)前月表和下月表", databaseName));
///基礎(chǔ)數(shù)據(jù)庫名
string nowbeforstr = nowitem.Substring(0, nowitem.IndexOf(nowtablePostfix));
// 是否已經(jīng)創(chuàng)建
bool isCreated = false;
if (nextDatatables != null && nextDatatables.Count > 0)
{
foreach (var nextitem in nextDatatables)
{
// 基礎(chǔ)數(shù)據(jù)庫名
string nextbeforstr = nowitem.Substring(0, nextitem.IndexOf(nexttablePostfix));
if (nowbeforstr == nextbeforstr)
{
isCreated = true;
ShowBusiness.ShowMsg(string.Format("處理【{0}】數(shù)據(jù)庫 表{1}已經(jīng)創(chuàng)建", databaseName, nextitem));
}
}
}
?
if (!isCreated)
{
string creatTableName = nowitem.Replace(nowtablePostfix, nexttablePostfix);
ShowBusiness.ShowMsg(string.Format("處理【{0}】數(shù)據(jù)庫 創(chuàng)建表{1}", databaseName, creatTableName));
DCreatTableFromDatabase creatTableFromDatabase = new DCreatTableFromDatabase(databaseName, nowitem, creatTableName);
creatTableFromDatabase.Execute();
int results = creatTableFromDatabase.Result;
}
}
}
?
#endregion
}
?
????????
第一次發(fā)博客,先發(fā)上去看看。????
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注