【发布时间】:2018-01-27 00:28:54
【问题描述】:
我正在尝试优化我的 InnoDB 表上 MariaDB (10.0.31) 上的大型 INSERT 查询的速度。
这是表格的结构(1.31 亿行):
Field__ Type___ Null Key Default Extra
ID_num_ bigint(45) NO PRI NULL
Content varchar(250)YES NULL
User_ID bigint(24) NO MUL NULL
Location varchar(70) YES NULL
Date_creat datetime NO MUL NULL
Retweet_ct int(7) NO NULL
isRetweet tinyint(1) NO NULL
hasReetwet tinyint(1) NO NULL
Original bigint(45) YES NULL
Url____ varchar(150)YES NULL
Favorite_c int(7) NO NULL
Selected int(11) NO 0
Sentiment int(11) NO 0
这是CREATE TABLE的输出:
CREATE TABLE `Twit` (
`ID_num` bigint(45) NOT NULL,
`Content` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`User_ID` bigint(24) NOT NULL,
`Location` varchar(70) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`Date_create` datetime NOT NULL,
`Retweet_count` int(7) NOT NULL,
`isRetweet` tinyint(1) NOT NULL,
`hasReetweet` tinyint(1) NOT NULL,
`Original` bigint(45) DEFAULT NULL,
`Url` varchar(150) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`Favorite_count` int(7) NOT NULL,
`Selected` int(11) NOT NULL DEFAULT '0',
`Sentiment` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID_num`),
KEY `User_ID` (`User_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
这是索引的结构:
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
Twit 0 PRIMARY 1 ID_num A 124139401 NULL NULL BTREE
Twit 1 User_ID 1 User_ID A 535083 NULL NULL BTREE
这里是show engine innodb status:
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 8942256128; in additional pool allocated 0
Total memory allocated by read views 184
Internal hash tables (constant factor + variable factor)
Adaptive hash index 141954688 (141606424 + 348264)
Page hash 4426024 (buffer pool 0 only)
Dictionary cache 35656039 (35403184 + 252855)
File system 845872 (812272 + 33600)
Lock system 21251648 (21250568 + 1080)
Recovery system 0 (0 + 0)
Dictionary memory allocated 252855
Buffer pool size 524286
Buffer pool size, bytes 8589901824
Free buffers 448720
Database pages 75545
Old database pages 27926
Modified db pages 0
Percent of dirty pages(LRU & free pages): 0.000
Max dirty pages percent: 75.000
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 74639, created 906, written 39133
0.12 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 999 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 75545, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
我使用以下 Python 代码从第 3 方源下载数据,然后用它填充我的表格:
add_twit = (" INSERT INTO Table (ID_num, Content,....) VALUES (%s, %s, ....)")
testtime=0
t0 = time.time()
data_twit = []
#### Data Retrieving ####
for page in limit_handled(...):
for status in page:
data_twit.append(processed_tweet)
####
##### MySQL Insert
tt0 = time.time()
cursorSQL.executemany(add_twit, data_twit)
testtime += time.time() - tt0
####
cnx.commit()
print('Total_TIME ' + str(time.time()-t0))
print('Sqlexecute_TIME ' + str(testtime))
代码做了什么:
它从 3rd 方提供者那里获取 twits,其中 16 页,每页有 200 个 twits(状态),因此每个迭代(用户)总共要向表中添加 3200 行。我尝试在每条推文中插入一个查询(使用cursorSQL.execute(add_twit, data_twit),并且在列表中也包含 200 条推文的 16 个查询,但最快的几秒钟是使用优化的 cursorSQL.executemany 函数对 3200 条推文进行查询。
对于 3200 条推文,下载它们大约需要 10 秒,将它们写入数据库大约需要 75 秒,考虑到一条推文(行)当前在表中占用 0.2ko,这似乎很多,因此 3200 只有 640 Ko .不应该花 75 秒...
使用iotop 监控磁盘使用情况时会发生什么:
- 在代码的数据检索部分期间(第一次迭代之后):
- 读取 = 0.00 B/s
- 写入 = 6.50 M/s
在大插入后,磁盘实际上会以 6Mbs/s 的速率持续写入几分钟
-
在代码的 SQL-Insert 部分:
- 读取 = 1.5 M/s
- 写入 = 300 K/s
看起来磁盘读取(我猜是为了索引目的?)使写入速率下降。
我尝试了什么:
-
尝试拆分插入查询(而不是1*3200行我尝试了16*200行和3200*1行,没有改变任何东西,1*3200稍微快一点)
李> 优化表(速度提高 15%)
删除不必要的索引
我的问题:
- 为什么当我提交 INSERT 查询而不是写入时磁盘开始读取?有没有办法防止这种情况发生?
删除所有 INDEX 是否有助于加快 INSERT?
我是否需要删除主键(不是列,只是其上的唯一索引),即使这听起来是个坏主意,并且 (MySQL slows down after INSERT) 建议不要这样做?
- 还有其他建议吗?
- 另外,为什么磁盘在大插入数分钟后仍以 6.00 Mb/s 的速度写入?
【问题讨论】:
-
网络延迟如何?您的数据库是否与前端进程位于同一台机器上?
-
@joop ,不,不是,但它在同一个本地网络上。读取查询非常有效。我认为网络非常高效且延迟低。
-
嗯,在
-
请提供
SHOW CREATE TABLE -
请提供插入的SQL示例;并非所有人都知道从 Python 到 SQL 的映射。
标签: python mysql indexing mariadb sql-insert