- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
本文末尾附加的 Python3 脚本创建了一个包含 5 个 INT
列的简单表,其中 3 个带有索引。
然后它使用 multi-row inserts 来填充表格。
一开始,它每秒能够插入大约 10000 行。
Took 0.983 s to INSERT 10000 rows, i.e. performance = 10171 rows per second.
Took 0.879 s to INSERT 10000 rows, i.e. performance = 11376 rows per second.
Took 0.911 s to INSERT 10000 rows, i.e. performance = 10982 rows per second.
Took 1.180 s to INSERT 10000 rows, i.e. performance = 8477 rows per second.
Took 1.030 s to INSERT 10000 rows, i.e. performance = 9708 rows per second.
Took 1.114 s to INSERT 10000 rows, i.e. performance = 8975 rows per second.
但是当表已包含大约 1000000 行时,性能会下降到每秒大约 2000 行。
Took 3.648 s to INSERT 10000 rows, i.e. performance = 2741 rows per second.
Took 3.026 s to INSERT 10000 rows, i.e. performance = 3305 rows per second.
Took 5.495 s to INSERT 10000 rows, i.e. performance = 1820 rows per second.
Took 6.212 s to INSERT 10000 rows, i.e. performance = 1610 rows per second.
Took 5.952 s to INSERT 10000 rows, i.e. performance = 1680 rows per second.
Took 4.872 s to INSERT 10000 rows, i.e. performance = 2053 rows per second.
作为比较:当使用 PostgreSQL 而不是 CockroachDB 时,性能始终约为每秒 40000 行。
Took 0.212 s to INSERT 10000 rows, i.e. performance = 47198 rows per second.
Took 0.268 s to INSERT 10000 rows, i.e. performance = 37335 rows per second.
Took 0.224 s to INSERT 10000 rows, i.e. performance = 44548 rows per second.
Took 0.307 s to INSERT 10000 rows, i.e. performance = 32620 rows per second.
Took 0.234 s to INSERT 10000 rows, i.e. performance = 42645 rows per second.
Took 0.262 s to INSERT 10000 rows, i.e. performance = 38124 rows per second.
Took 0.301 s to INSERT 10000 rows, i.e. performance = 33254 rows per second.
Took 0.220 s to INSERT 10000 rows, i.e. performance = 45547 rows per second.
Took 0.260 s to INSERT 10000 rows, i.e. performance = 38399 rows per second.
Took 0.222 s to INSERT 10000 rows, i.e. performance = 45136 rows per second.
Took 0.213 s to INSERT 10000 rows, i.e. performance = 46950 rows per second.
Took 0.211 s to INSERT 10000 rows, i.e. performance = 47436 rows per second.
使用 CockroachDB 时有没有办法提高性能?
由于表是连续填充的,因此不能先填充表,然后再添加索引。
db_insert_performance_test.py
:
import random
from timeit import default_timer as timer
import psycopg2
def init_table(cur):
"""Create table and DB indexes"""
cur.execute("""
CREATE TABLE entities (a INT NOT NULL, b INT NOT NULL,
c INT NOT NULL, d INT NOT NULL,
e INT NOT NULL);""")
cur.execute('CREATE INDEX a_idx ON entities (a);')
cur.execute('CREATE INDEX b_idx ON entities (b);')
cur.execute('CREATE INDEX c_idx ON entities (c);')
# d and e does not need an index.
def create_random_event_value():
"""Returns a SQL-compatible string containing a value tuple"""
def randval():
return random.randint(0, 100000000)
return f"({randval()}, {randval()}, {randval()}, {randval()}, {randval()})"
def generate_statement(statement_template, rows_per_statement):
"""Multi-row insert statement for 200 random entities like this:
INSERT INTO entities (a, b, ...) VALUES (1, 2, ...), (6, 7, ...), ...
"""
return statement_template.format(', '.join(
create_random_event_value()
for i in range(rows_per_statement)))
def main():
"""Write dummy entities into db and output performance."""
# Config
database = 'db'
user = 'me'
password = 'pwd'
host, port = 'cockroach-db', 26257
#host, port = 'postgres-db', 5432
rows_per_statement = 200
statements_per_round = 50
rounds = 100
statement_template = 'INSERT INTO entities (a, b, c, d, e) VALUES {}'
# Connect to DB
conn = psycopg2.connect(database=database, user=user, password=password,
host=host, port=port)
conn.set_session(autocommit=True)
cur = conn.cursor()
init_table(cur)
for _ in range(rounds):
# statements_per_round multi-row INSERTs
# with rows_per_statement rows each
batch_statements = [generate_statement(statement_template,
rows_per_statement)
for _ in range(statements_per_round)]
# Measure insert duration
start = timer()
for batch_statement in batch_statements:
cur.execute(batch_statement)
duration = timer() - start
# Calculate performance
row_count = rows_per_statement * statements_per_round
rows_per_second = int(round(row_count / duration))
print('Took {:7.3f} s to INSERT {} rows, '
'i.e. performance = {:>6} rows per second.'
''.format(duration, row_count, rows_per_second), flush=True)
# Close the database connection.
cur.close()
conn.close()
if __name__ == '__main__':
main()
为了快速重现我的结果,这里有一个docker-compose.yml
:
version: '2.4'
services:
cockroach-db:
image: cockroachdb/cockroach:v2.0.3
command: start --insecure --host cockroach-db --vmodule=executor=2
healthcheck:
test: nc -z cockroach-db 26258
cockroach-db-init:
image: cockroachdb/cockroach:v2.0.3
depends_on:
- cockroach-db
entrypoint: /cockroach/cockroach sql --host=cockroach-db --insecure -e "CREATE DATABASE db; CREATE USER me; GRANT ALL ON DATABASE db TO me;"
postgres-db:
image: postgres:10.4
environment:
POSTGRES_USER: me
POSTGRES_PASSWORD: pwd
POSTGRES_DB: db
healthcheck:
test: nc -z postgres-db 5432
db-insert-performance-test:
image: python:3.6
depends_on:
- cockroach-db-init
- postgres-db
volumes:
- .:/code
working_dir: /
entrypoint: bash -c "pip3 install psycopg2 && python3 code/db_insert_performance_test.py"
要开始测试,只需运行docker-compose up db-insert-performance-test
。
最佳答案
CockroachDB 将数据存储在“范围”内,当范围达到 64MB 时,范围就会被分割。最初,该表适合一个范围,因此每次插入都是单范围操作。 Range拆分后,每次插入都需要涉及多个Range来更新表和索引;因此,预计性能会下降。
关于sql - 如何提高 CockroachDB 的 INSERT 性能(每秒行数)(与 PostgreSQL 相比大约慢 20 倍),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51000680/
自己试试看: import pandas as pd s=pd.Series(xrange(5000000)) %timeit s.loc[[0]] # You need pandas 0.15.1
我最近开始使用 Delphi 中的 DataSnap 来生成 RESTful Web 服务。在遵循 Marco Cantu 本人和互联网上其他几个人的指导后,我成功地使整个“链条”正常工作。 但是有一
我一直在为操作系统类(class)编写以下代码,但结果有些奇怪。该代码创建x线程并同时运行它们,以便将两个平方矩阵相乘。每个线程将输入矩阵的Number_of_rows/Number_of_threa
我正在尝试确定何时使用 parallel包以加快运行某些分析所需的时间。我需要做的一件事是创建矩阵,比较具有不同行数的两个数据框中的变量。我在 StackOverflow 上问了一个关于有效方法的问题
我最近对我的代码进行了一些清理,并在此过程中更改了此内容(不完全是真实的代码): read = act readSTRef test1 term i var = do t v^!terms.
我正在计时查询和同一个查询的执行时间,分页。 foreach (var x in productSource.OrderBy(p => p.AdminDisplayName) .Wher
我正在开发一个项目 (WPF),我有一个 Datagrid 从数据库加载超过 5000 条记录,所以我使用 BackgroundWorker 来通知用户数据正在加载,但它太慢了,我需要等待将近 2分钟
我在查询中添加 ORDER BY 时遇到问题。没有 ORDER BY 查询大约需要 26ms,一旦我添加 ORDER BY,它大约需要 20s。 我尝试了几种不同的方法,但似乎可以减少时间。 尝试 F
我是 Android 开发新手,遇到了性能问题。当我的 GridView 有太多项目时,它会变得有点慢。有什么方法可以让它运行得更快一些吗? 这是我使用的代码: 适配器: public class C
这里的要点是: 1.设置query_cache_type = 0;重置查询缓存; 2.在 heidisql(或任何其他客户端 UI)中运行任何查询 --> 执行,例如 45 毫秒 3.使用以下代码运行
想象下表: CREATE TABLE drops( id BIGSERIAL PRIMARY KEY, loc VARCHAR(5) NOT NULL, tag INT NOT
我的表 test_table 中的示例数据: date symbol value created_time 2010-01-09 symbol1
首先,如果已经有人问过这个问题,我深表歉意,至少我找不到任何东西。 无论如何,我将每 5 分钟运行一次 cron 任务。该脚本加载 79 个外部页面,而每个页面包含大约 200 个我需要在数据库中检查
我有下面的 SQL 代码,它来自 MySQL 数据库。现在它给了我期望的结果,但是查询很慢,我想我应该在进一步之前加快这个查询的速度。 表agentstatusinformation有: PKEY(主
我需要获取一个对象在 Core Data 中数千个其他对象之间的排名。现在,这是我的代码: - (void)rankMethod { //Fetch all objects NSFet
我正在编写一个应用程序,我需要在其中读取用户的地址簿并显示他所有联系人的列表。我正在测试的 iPhone 有大约 100 个联系人,加载联系人确实需要很多时间。 ABAddressBookRef ad
我正在使用 javascript 将 160 行添加到包含 10 列的表格中。如果我这样做: var cellText = document.createTextNode(value); cell.a
我是 Swift 的新手,我已经设置了一个 tableView,它从 JSON 提要中提取数据并将其加载到表中。 表格加载正常,但是当表格中有超过 10 个单元格时,它会变得缓慢且有些滞后,特别是它到
我在 InitializeCulture 和 Page_PreInit 事件之间的 asp.net 页面中遇到性能问题。当我重写 DeterminePostBackMode() 时,我发现问题出在 b
我在 Hetzner 上有一个带有 256GB RAM 6 个 CPU(12 个线程) 的专用服务器,它位于德国。我有 CENTOS 7.5。 EA4。 我的问题是 SSL。每天大约 2 小时,我们在
我是一名优秀的程序员,十分优秀!