在MySQL 8.0 之前, 我們假設一下有一條爛SQL,
目前創新互聯已為成百上千的企業提供了網站建設、域名、虛擬空間、綿陽服務器托管、企業網站設計、順城網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。
mysqlselect * from t1 order by rand() ;
以多個線程在跑,導致CPU被跑滿了,其他的請求只能被阻塞進不來。那這種情況怎么辦?
大概有以下幾種解決辦法:
設置max_execution_time 來阻止太長的讀SQL。那可能存在的問題是會把所有長SQL都給KILL 掉。有些必須要執行很長時間的也會被誤殺。
自己寫個腳本檢測這類語句,比如order by rand(), 超過一定時間用Kill query thread_id 給殺掉。
那能不能不要殺掉而讓他正常運行,但是又不影響其他的請求呢?
那mysql 8.0 引入的資源組(resource group,后面簡寫微RG)可以基本上解決這類問題。
比如我可以用 RG 來在SQL層面給他限制在特定的一個CPU核上,這樣我就不管他,讓他繼續運行,如果有新的此類語句,讓他排隊好了。
為什么說基本呢?目前只能綁定CPU資源,其他的暫時不行。
那我來演示下如何使用RG。
創建一個資源組user_ytt. 這里解釋下各個參數的含義,
type = user 表示這是一個用戶態線程,也就是前臺的請求線程。如果type=system,表示后臺線程,用來限制mysql自己的線程,比如Innodb purge thread,innodb read thread等等。
vcpu 代表cpu的邏輯核數,這里0-1代表前兩個核被綁定到這個RG。可以用lscpu,top等列出自己的CPU相關信息。
thread_priority 設置優先級。user 級優先級設置大于0。
mysqlmysql create resource group user_ytt type = user ?vcpu = 0-1 thread_priority=19 enable;Query OK, 0 rows affected (0.03 sec)
RG相關信息可以從 information_schema.resource_groups 系統表里檢索。
mysqlmysql select * from information_schema.resource_groups;+---------------------+---------------------+------------------------+----------+-----------------+| RESOURCE_GROUP_NAME | RESOURCE_GROUP_TYPE | RESOURCE_GROUP_ENABLED | VCPU_IDS | THREAD_PRIORITY |+---------------------+---------------------+------------------------+----------+-----------------+| USR_default ? ? ? ? | USER ? ? ? ? ? ? ? ?| ? ? ? ? ? ? ? ? ? ? ?1 | 0-3 ? ? ?| ? ? ? ? ? ? ? 0 || SYS_default ? ? ? ? | SYSTEM ? ? ? ? ? ? ?| ? ? ? ? ? ? ? ? ? ? ?1 | 0-3 ? ? ?| ? ? ? ? ? ? ? 0 || user_ytt ? ? ? ? ? ?| USER ? ? ? ? ? ? ? ?| ? ? ? ? ? ? ? ? ? ? ?1 | 0-1 ? ? ?| ? ? ? ? ? ? ?19 |+---------------------+---------------------+------------------------+----------+-----------------+3 rows in set (0.00 sec)
我們來給語句select guid from t1 group by left(guid,8) order by rand() 賦予RG user_ytt。
mysql show processlist;+-----+-----------------+-----------+------+---------+-------+------------------------+-----------------------------------------------------------+| Id ?| User ? ? ? ? ? ?| Host ? ? ?| db ? | Command | Time ?| State ? ? ? ? ? ? ? ? ?| Info ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|+-----+-----------------+-----------+------+---------+-------+------------------------+-----------------------------------------------------------+| ? 4 | event_scheduler | localhost | NULL | Daemon ?| 10179 | Waiting on empty queue | NULL ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|| 240 | root ? ? ? ? ? ?| localhost | ytt ?| Query ? | ? 101 | Creating sort index ? ?| select guid from t1 group by left(guid,8) order by rand() || 245 | root ? ? ? ? ? ?| localhost | ytt ?| Query ? | ? ? 0 | starting ? ? ? ? ? ? ? | show processlist ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|+-----+-----------------+-----------+------+---------+-------+------------------------+-----------------------------------------------------------+3 rows in set (0.00 sec)
找到連接240對應的thread_id。
mysqlmysql select thread_id from performance_schema.threads where processlist_id = 240;+-----------+| thread_id |+-----------+| ? ? ? 278 |+-----------+1 row in set (0.00 sec)
給這個線程278賦予RG user_ytt。沒報錯就算成功了。
mysqlmysql set resource group user_ytt for 278;Query OK, 0 rows affected (0.00 sec)
當然這個是在運維層面來做的,我們也可以在開發層面結合 MYSQL HINT 來單獨給這個語句賦予RG。比如:
mysqlmysql select /*+ resource_group(user_ytt) */guid from t1 group by left(guid,8) order by rand()....8388602 rows in set (4 min 46.09 sec)
RG的限制:
Linux 平臺上需要開啟 CAPSYSNICE 特性。比如我機器上用systemd 給mysql 服務加上
systemctl edit mysql@80 [Service]AmbientCapabilities=CAP_SYS_NICE
mysql 線程池開啟后RG失效。
freebsd,solaris 平臺thread_priority 失效。
目前只能綁定CPU,不能綁定其他資源。
#include QCoreApplication
#include "thread.h"
#include QVector
#include QDebug
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QVectorThread* vector;
Thread *thread;
//創建多個線程,并start
for(int i=0;i10;i++){
thread=new Thread;
vector.append(thread);
thread-set(i);
thread-start();
}
//等待所有線程執行完,然后刪除線程
foreach(thread,vector){
thread-wait();
}
foreach(thread,vector){
delete thread;
}
return a.exec();
}
-------------------------------------------------------------------------------------------------
#include "thread.h"
Thread::Thread(QObject *parent) : QThread(parent)
{
}
void Thread::run()
{
begin();
}
//為每個線程創建一個連接名
void Thread::set(int a)
{
connectionName=QString::number(a);
}
void Thread::connectionDatabase(QString dbName)
{
QSqlDatabasedb=QSqlDatabase::addDatabase("QMYSQL",connectionName);
db.setHostName("localhost");
db.setDatabaseName(dbName);
db.setUserName("root");
db.setPassword("");
if(!db.open())
qDebug()"db open fail";
}
void Thread::begin()
{
QString dbName="learnsql";
connectionDatabase(dbName);
QSqlDatabase db=QSqlDatabase::database(connectionName);
db.transaction(); //開啟事物
QSqlQuery query(db);
//向表student中插入10000條數據
for(int i=1;i=10000;i++){
query.exec("insert into student values(1)");
}
db.commit(); //提交事物
}
數組吧,直接把數組轉字符串啊
implode() 函數返回由數組元素組合成的字符串。(適合一維數組)
$arr = array('Hello', 'World', 'I', 'love', 'Shanghai');
1 echo implode(" ",$arr);//加空格
the result : Hello World I love Shanghai
2 echo implode(",",$arr);//加逗號
the result : Hello,World,I,love,Shanghai
轉換數組為字符串后插入數據庫就可以了。
分享文章:php多線程寫入數據庫6 php實現多線程
地址分享:http://m.kartarina.com/article30/dogesso.html
成都網站建設公司_創新互聯,為您提供面包屑導航、網站導航、品牌網站設計、虛擬主機、網站改版、網頁設計公司
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯