C++ 插入序列的优化
我有一个实时应用程序,它处理信息并将其记录到MySQL数据库(实际上是MySQL的分支MariaDB)。它每天进行大约150万次插入+15万次删除 我在性能上有很大的问题,不知道如何使它更好地工作 应用程序的基本结构是,我有一个producer类,它将结构推送到线程安全的deque。下面的代码C++ 插入序列的优化,c++,mysql,optimization,insert,mariadb,C++,Mysql,Optimization,Insert,Mariadb,我有一个实时应用程序,它处理信息并将其记录到MySQL数据库(实际上是MySQL的分支MariaDB)。它每天进行大约150万次插入+15万次删除 我在性能上有很大的问题,不知道如何使它更好地工作 应用程序的基本结构是,我有一个producer类,它将结构推送到线程安全的deque。下面的代码 #include "dbUserQueue.h" dbUserQueue::~dbUserQueue() { } void dbUserQueue::createConnection() {
#include "dbUserQueue.h"
dbUserQueue::~dbUserQueue() {
}
void dbUserQueue::createConnection()
{
sql::Driver * driver = sql::mysql::get_driver_instance();
std::auto_ptr< sql::Connection > newCon(driver->connect(dbURL, dbUser, dbPass));
con = newCon;
std::auto_ptr< sql::Statement > stmt(con->createStatement());
stmt->execute("USE twitter");
}
inline void dbUserQueue::updateStatement(const std::string & value,
std::auto_ptr< sql::PreparedStatement> & stmt, const int index)
{
if(value != "\0") stmt->setString(index, value);
else stmt->setNull(index,sql::DataType::VARCHAR);
}
inline void dbUserQueue::updateStatement(const boost::int64_t & value,
std::auto_ptr< sql::PreparedStatement> & stmt, const int index)
{
if(value != -1) stmt->setInt64(index,value);
else stmt->setNull(index,sql::DataType::BIGINT);
}
inline void dbUserQueue::updateStatement(const bool value,
std::auto_ptr< sql::PreparedStatement> & stmt, const int index)
{
stmt->setBoolean(index, value);
}
inline void dbUserQueue::updateStatement(const int value,
std::auto_ptr< sql::PreparedStatement> & stmt, const int index)
{
if(value != -1) stmt->setInt(index,value);
else stmt->setNull(index,sql::DataType::INTEGER);
}
inline void dbUserQueue::updateStatementDateTime(const std::string & value,
std::auto_ptr< sql::PreparedStatement> & stmt, const int & index)
{
if(value != "\0") stmt->setDateTime(index, value);
else stmt->setNull(index,sql::DataType::DATE);
}
/*
* This method creates a database connection
* and then creates a new thread to process the incoming queue
*/
void dbUserQueue::start() {
createConnection();
if(con->isClosed() == false)
{
insertStmt = std::auto_ptr< sql::PreparedStatement>(con->prepareStatement("\
insert ignore into users(contributors_enabled, created_at, \
description, favourites_count, followers_count, \
following, friends_count, geo_enabled, id, lang, listed_count, location, \
name, notifications, screen_name, show_all_inline_media, statuses_count, \
url, utc_offset, verified) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"));
}
thread = boost::thread(&dbUserQueue::processLoop, this);
}
/*
* Stops the thread once it is finished processing the information
*/
void dbUserQueue::join(){
thread.interrupt();
thread.join();
}
/*
* The worker function of the thread.
* Pops items from the queue and updates the database accordingly.
*/
void dbUserQueue::processLoop() {
user input;
int recordCount = 0;
con->setAutoCommit(false);
while (true) {
try {
if(recordCount >= 1000)
{
recordCount = 0;
con->commit();
}
// Insert all the data into the prepared statement
if (userQ.wait_and_pop(input)) {
updateStatement(input.contributors_enabled, insertStmt, 1);
updateStatementDateTime(input.created_at, insertStmt, 2);
updateStatement(input.description, insertStmt, 3);
updateStatement(input.favourites_count, insertStmt, 4);
updateStatement(input.followers_count, insertStmt, 5);
updateStatement(input.following, insertStmt, 6);
updateStatement(input.friends_count, insertStmt, 7);
updateStatement(input.geo_enabled, insertStmt, 8);
updateStatement(input.id, insertStmt, 9);
updateStatement(input.lang, insertStmt, 10);
updateStatement(input.listed_count, insertStmt, 11);
updateStatement(input.location, insertStmt, 12);
updateStatement(input.name, insertStmt, 13);
updateStatement(input.notifications, insertStmt, 14);
updateStatement(input.screenName, insertStmt, 15);
updateStatement(input.show_all_inline_media, insertStmt, 16);
updateStatement(input.statuses_count, insertStmt, 17);
updateStatement(input.url, insertStmt, 18);
updateStatement(input.utc_offset, insertStmt, 19);
updateStatement(input.verified, insertStmt, 20);
insertStmt->executeUpdate();
insertStmt->clearParameters();
recordCount++;
continue;
}
} catch (std::exception & e) {
}
}// end of while
// Close the statements and the connection before exiting
insertStmt->close();
con->commit();
if(con->isClosed() == false)
con->close();
}
关于问题域的一些注释。要插入和/或删除的消息在一天中随机出现,平均每秒约20次插入/删除,突发次数要高得多,但没有理由不在短时间内对更新进行排队,只要队列不会变大
当前插入数据的表中有大约5200万条记录。下面是MySQL表信息
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL,
`contributors_enabled` tinyint(4) DEFAULT '0',
`created_at` datetime NOT NULL,
`description` varchar(255) DEFAULT NULL,
`favourites_count` int(11) NOT NULL,
`followers_count` int(11) DEFAULT NULL,
`following` varchar(255) DEFAULT NULL,
`friends_count` int(11) NOT NULL,
`geo_enabled` tinyint(4) DEFAULT '0',
`lang` varchar(255) DEFAULT NULL,
`listed_count` int(11) DEFAULT NULL,
`location` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`notifications` varchar(45) DEFAULT NULL,
`screen_name` varchar(45) NOT NULL,
`show_all_inline_media` tinyint(4) DEFAULT NULL,
`statuses_count` int(11) NOT NULL,
`url` varchar(255) DEFAULT NULL,
`utc_offset` int(11) DEFAULT NULL,
`verified` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=MARIA DEFAULT CHARSET=latin1 CHECKSUM=1 PAGE_CHECKSUM=1 TRANSACTIONAL=1
您可以将代码更改为批量插入,而不是一次插入一行 我认为你发布了错误的表格结构。你发布了
推文
,但你正在向用户插入。@Larry:谢谢你的关注。我对这两个部分都有问题,插入推文和插入用户。我刚刚发布了一个,因为代码非常相似。好吧,马上你就把同一列索引了两次。主键(id)将在(id)上生成一个唯一的索引。感谢Larry,这将性能提高了约30-40%。我使用了一个mysql gui来创建表,您必须选中每个字段旁边的复选框,然后单击主键和唯一键,这就解释了我是如何获得这两个字段的。还是粗心的错误。你建议怎么做?在insert语句中列出多个值。您是准备好一条语句,一次插入100条记录,还是动态构建SQL字符串,然后每100条记录执行一次?(我只是随机选择了100个,可以是任何数字)。我尝试过使用事务和预处理语句,因此我的[有限]理解应该有助于缓解其中一些问题,因为在我提交之前,它实际上不会更新数据库,使其仅写入一次,而预处理语句通过传递变量来减少带宽。我没有使用预处理语句,但是通过将insert语句生成为字符串并将该字符串传递给mysqlpp::Query。我希望您可以使用准备好的语句以类似的方式进行批量插入。如果对每一行执行insert语句,我认为客户机和服务器之间的通信量会大得多,这一定会降低速度。因此,建议使用一个带有设置数量的“值”字段的准备好的语句。类似于这样的内容:PrepareStatement(“插入tbl_名称(a、b、c)值(?,,,,,,,,,,,,,,,,,,,,,,,”);,将多条记录的值赋给一条语句,然后执行该语句。我认为这是非常“笨重”,并认为这不是最佳的解决方案。我会尝试一下,看看它是否能提高性能,然后再给你回复。如果有一种方法可以自动插入多个记录,那就太好了。我尝试了这个方法,它极大地提高了吞吐量。谢谢
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL,
`contributors_enabled` tinyint(4) DEFAULT '0',
`created_at` datetime NOT NULL,
`description` varchar(255) DEFAULT NULL,
`favourites_count` int(11) NOT NULL,
`followers_count` int(11) DEFAULT NULL,
`following` varchar(255) DEFAULT NULL,
`friends_count` int(11) NOT NULL,
`geo_enabled` tinyint(4) DEFAULT '0',
`lang` varchar(255) DEFAULT NULL,
`listed_count` int(11) DEFAULT NULL,
`location` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`notifications` varchar(45) DEFAULT NULL,
`screen_name` varchar(45) NOT NULL,
`show_all_inline_media` tinyint(4) DEFAULT NULL,
`statuses_count` int(11) NOT NULL,
`url` varchar(255) DEFAULT NULL,
`utc_offset` int(11) DEFAULT NULL,
`verified` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=MARIA DEFAULT CHARSET=latin1 CHECKSUM=1 PAGE_CHECKSUM=1 TRANSACTIONAL=1