- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我现在在托管服务器上运行一个 mysql docker 容器镜像,具有 2 核 14gb ram 8000 iops max我经常选择和更新查询
我确实使用 python 和 mysql-connector
我在开始时初始化一个连接我在开始时打开连接的性能更好,并且仅在退出时关闭它
def initsql():
logger.log('initsql()')
global cnx
cnx = mysql.connector.connect(user='blabla', password='blabla',
host='blablahost',
database='blablabladb')
def closesql():
logger.log('closesql()')
global cnx
cnx.close()
现在有 2 个非常慢的函数:
def sqlSetModus(modus):
logger.log('sqlSetModus()')
t0 = time.time()
try:
internet_on() #Function check if there is a internet connection if not raise Exception()
global cnx
cursor = cnx.cursor()
global logintoken
currentdate = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if modus == 'offline':
updateModus = '''UPDATE accounts SET inuse=0, token='none', was_online = '%s', modus = '%s' WHERE token = '%s' ''' % (currentdate, modus, logintoken)
else:
updateModus = '''UPDATE accounts SET was_online = '%s', modus = '%s' WHERE token = '%s' ''' % (currentdate, modus, logintoken)
cursor.execute(updateModus)
cnx.commit()
logger.success("sqlSetModus after {} Sekunden".format(time.time() - t0))
return True
except Exception:
logger.error("Error sqlSetModus after {} Sekunden".format(time.time() - t0))
raise Exception
finally:
cursor.close()
def sqlUpdatePoints(company):
logger.log('sqlUpdatePoints()')
t0 = time.time()
try:
internet_on()
global cnx
cursor = cnx.cursor()
global sessiontoken
currentdate = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
pointsvalue = 0.55
updateOnline = '''UPDATE accounts SET was_online = '%s', this_points = this_points + '%s' WHERE token = '%s' ''' % (currentdate, pointsvalue, logintoken)
cursor.execute(updateOnline)
updatePoints = '''UPDATE points SET points = points + '%s' WHERE company = 'companyXYZ' ''' % (pointvalue,)
cursor.execute(updatePoints)
updateAccount = '''INSERT INTO statistic (company, datum, website) VALUES ('companyXYZ', '%s', '%s') ON DUPLICATE KEY UPDATE counter=counter+1''' % (currentdate, company)
cursor.execute(updateAccount)
cnx.commit()
logger.success("sqlSetAfterSuccesplay after {} Sekunden".format(time.time() - t0))
return True
except Exception:
logger.error("Error sqlSetAfterSuccesplay after {} Sekunden".format(time.time() - t0))
raise Exception
finally:
cursor.close()
我当然有更多的代码和更多的 sql 查询,比如简单的选择,但这些确实工作得很快
这两个函数的耗时总是超过 10 秒
sqlUpdatePoints 次:
[17:38:57.790] sqlUpdatePoints after 13.4862029552 Sekunden
[17:39:48.294] sqlUpdatePoints after 14.960242033 Sekunden
sqlSetModus 次:
[17:38:44.051] sqlSetModus after 14.4736759663 Sekunden
[17:39:31.702] sqlSetModus after 13.1622648239 Sekunden
[17:40:09.977] sqlSetModus after 16.7646770477 Sekunden
简单的选择查询时间:
[17:39:53.213] sqlGetActiveAccount after 0.741001844406 Sekunden
[17:39:02.695] sqlGetActiveAccount after 0.737174034119 Sekunden
[17:38:12.800] sqlGetActiveAccount after 0.7693400383 Sekunden
你必须知道我同时运行了 100 个此代码的实例,这意味着这些是打开 100 个连接的时间并且经常对数据库运行 sqlUpdatePoints + sqlSetModus 和简单查询
表格看起来像这样:
nr = int (autoincrement unique)
username = varchar
password = varchar
inuse = int
token = varchar
was_online = datetime
points = float
modus = enum
nr | username | password | inuse | token | was_online | this_points | modus
最佳答案
你没有说你已经有哪些索引,所以我假设你没有:
对于 sqlSetModus()
函数,您需要索引:
create index ix_account_token on accounts (token);
对于 sqlUpdatePoints()
函数,您需要索引:
create index ix_points_company on points (company);
您对过滤器使用相等性 (=
),因此使用这些索引,您的搜索(更新)应该非常快。
关于Python MYSQL 查询太慢,请问是什么原因。用好的索引优化可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51329049/
我用 cudaMemcpy()一次将 1GB 的数据精确复制到设备。这需要 5.9 秒。反之则需要 5.1 秒。这是正常的吗?函数本身在复制之前有这么多开销吗? 理论上,PCIe 总线的吞吐量至少应为
我正在尝试读取图像的大小并在其高度大于 150 时调整其边距。但是在运行这段代码时,我总是在控制台中得到一个“0”: var coverImg; coverImg =
我正在开发一个 iPhone 应用程序,其中包含一些标准的“相机”功能。保存到相机胶卷真的太慢了,在iPhone 4上大约需要四秒钟。有什么办法可以提高速度吗? 如果您查看默认的 iPhone 相
我创建了一个将图像转换为灰度的类。但它的工作速度太慢了。有没有办法让它运行得更快? 这是我的类(class): @implementation PixelProcessing SYNTHESIZE_S
我使用以下代码,结果是正确的,但 gethostbyaddr 需要大约 30 秒。 function IPAddrToName(IPAddr: string): string; var So
我有以下案例, public class Test { private static final int MAX_NUMBER = 10_00_00; public static vo
我已经正确添加了所有必需的 JARS: Ucanaccess 3.0.4 commons-lang-2.6 commons-logging-1.1.1 hsqldbd jackcess-2.1.3 我
我为特定功能构建了一个多处理密码破解程序(使用单词列表),与使用单个进程相比,它减少了一半的时间。 最初的问题是,它会向您显示破解的密码并终止工作人员,但剩余的工作人员将继续工作,直到他们用完可哈希的
我在我的一个 JSP 中引入了 Sencha 网格。本地 sencha 相当快,但在外部服务器上它太慢了。 我在这里按照部署说明进行操作 http://docs.sencha.com/ext-js/4
我的查询加载时间有很大问题。在这种情况下,我需要 hg_ft_won 列(表:值)中的值,用于 home_team_id 和 away_team_id(表:匹配)。 它确实可以正常工作。加载只需要很长
我现在正在学习不同类型的排序,我发现,从某个点开始,我的快速排序算法根本无法快速工作。 这是我的代码: class QuickSort { // partitioning arr
为什么要模式 [0123]123456|98765 比在 Java 中执行 [0123]123456 然后 98765 慢两倍?所以单独搜索它们比用 OR 执行更快。有人有解释吗? UPD 查看带有结
我有带 Assets 的 Android 应用程序。它们包含 20,000 多个文件,其中大部分是简单的文本或 png 文件,分为不同的文件夹和子文件夹。1 个单个文件的最大大小为 500kb,其中
您好,我在查询中添加了 GROUP_CONCAT 函数,该函数终止了我的查询:/。我的查询是: SELECT u.username,a.user_id,a.id,a.text,a.lang as fr
我正在寻找优化查询的想法。 目前,我有一个 4M 行的表,我只想检索引用的最后 1000 行: SELECT * FROM customers_material_events WHERE refere
我在我的应用程序中使用 NSURLConnection,我在其中扫描条形码,通过 NSURLConnection 发送 XML,Java 服务向我发回 XML。我的问题是,使用 Wifi 时,响应时间
当我运行以下程序时,执行大约需要 7 到 8 分钟。我真的不确定我哪里弄错了,因为这个程序执行起来要花很多时间。 public class Test { public stat
我正在使用 NSFetchResultsController 从数据库中接收项目(有 80.000 个项目)。 这是我的谓词:@"(desc CONTAINS[cd] %@)", [any text]
我在 x_data 中有一个 3x2000 numpy 数组,在 y_data 中有一个 1x2000 numpy 数组,我将其传递给此函数 regress 以给我一条回归线。它工作正常。问题是我正在
我正在做一个项目,我需要改变图像的亮度和对比度,它是亮度而不是亮度。所以我一开始的代码是 for (int y = 0; y (y, x); // read pixel (0,0)
我是一名优秀的程序员,十分优秀!