簡體   English   中英

MySQL 'localhost' 數據庫在 InnoDB 引擎上運行速度非常慢。 切換到 MyISAM,速度提高了 20 倍。 但是什么引擎最適合我的數據呢?

[英]MySQL 'localhost' database is very slow running on InnoDB engine. Switched to MyISAM and its 20x faster. But what engine is the best for my data?

問題在帖子底部。

我在 Windows 10。使用 MySQL Workbench 8.0CE。

數據是1014行電影手稿。

字面意思是快 20 倍。 它從 InnoDB 上的 40 分鍾,到 MyISAM 上的 2 分鍾,按照 python 腳本運行。

from random import randint
from time import sleep
import requests
from bs4 import BeautifulSoup as bs
import json
import pymysql
import traceback
import logging
from tqdm import tqdm

logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)

mysql_code = "password"

def getData(id, script_raw):
    #print(script_raw)
    script_clean = remove_html_tags(script_raw).replace("'","''")
    #output_str = ''.join(c for c in script_clean if c.isprintable())
    #print(output_str)
    #print(script_clean_2)
    save_data(script_clean, id)

def remove_html_tags(text):
    """Remove html tags from a string"""
    import re
    clean = re.compile('<.*?>')
    return re.sub(clean, '', text)

def save_data(script_clean, id):
    try:
        conn = pymysql.connect(host='localhost', user='admin',
                               passwd=mysql_code, db='manuscriptproject')
        cur = conn.cursor()

        query = "UPDATE `clean_movie_script` SET `script_clean` = '%s'  WHERE (`id` = '%s');"
        final_query = query % (script_clean, id)

        cur.execute(final_query)
        conn.commit()
        cur.close()
        conn.close()
    except Exception as e:
        logging.info("Error with query for id : " + str(id))
        logging.error(traceback.format_exc())
        logging.error(e)

def get_non_populated_records():
    conn = pymysql.connect(host='localhost', user='admin',
                               passwd=mysql_code, db='manuscriptproject')
    cur = conn.cursor()
    cur.execute(
        "SElECT id, script FROM `movie_script` "
        "WHERE script IS NOT NULL "
        "ORDER BY id asc "
        "LIMIT 100000")
    data = list(cur.fetchall())
    conn.close()
    return data

if __name__ == "__main__":

    unpopulated_records = get_non_populated_records()

    for x in tqdm(unpopulated_records):
        try:
            getData(x[0], x[1])
        except Exception as e:
            print(e)

從 InnoDB 切換到 MyISAM,它改變了它定義我的“id”列的方式,從啟用 PK、NN、Unique 和自動增量的 INT 到僅啟用 NN 且默認表達式為“0”的 INT。 我現在也無法將引擎改回 InnoDB。

問題:我試圖了解哪種引擎最適合我的用例。 對於 200MB 的數據庫來說,2 分鍾似乎仍然很慢。 在線搜索,InnoDB 應該比 MyISAM 更快,這可能與我定義“id”列的方式有關——我就是想不通。

對整個程序只執行一次:

conn = pymysql.connect(...)

(這可能是任務中成本最高的部分,代碼似乎是在一個循環中完成的。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM