Python 加快MySQL对lastrowid的多个单次插入

Python 加快MySQL对lastrowid的多个单次插入,python,mysql,Python,Mysql,我有两张桌子,第一张: +-----------------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------+---------------+------+-----+---------+----------------+ | id

我有两张桌子,第一张:

+-----------------+---------------+------+-----+---------+----------------+
| Field           | Type          | Null | Key | Default | Extra          |
+-----------------+---------------+------+-----+---------+----------------+
| id              | bigint(20)    | NO   | PRI | NULL    | auto_increment |
| profile_id      | int(11)       | YES  |     | NULL    |                |
| landing_page_id | int(11)       | YES  |     | NULL    |                |
| keyword         | varchar(2083) | YES  |     | NULL    |                |
| unique_key      | varchar(200)  | YES  | UNI | NULL    |                |
+-----------------+---------------+------+-----+---------+----------------+
第二个看起来像:

+-----------------+------------+------+-----+---------+----------------+
| Field           | Type       | Null | Key | Default | Extra          |
+-----------------+------------+------+-----+---------+----------------+
| id              | bigint(20) | NO   | PRI | NULL    | auto_increment |
| profile_id      | int(11)    | YES  |     | NULL    |                |
| landing_page_id | int(11)    | YES  |     | NULL    |                |
| keyword_id      | int(11)    | YES  |     | NULL    |                |
| position        | int(11)    | YES  |     | NULL    |                |
| impressions     | int(11)    | YES  |     | NULL    |                |
| ctr             | float      | YES  |     | NULL    |                |
| clicks          | int(11)    | YES  |     | NULL    |                |
| metric_dates    | date       | YES  |     | NULL    |                |
+-----------------+------------+------+-----+---------+----------------+
它们通过第一个表中的
id
和第二个表中的
keyword\u id
连接

目前,我正在插入每个
关键字
,然后返回
lastrowid
。然后,在我收集所有关键字ID(
lastrowid
)的同时,第二个表的数据被批处理在一起,并在末尾以5000个为一批插入

正如您可以想象的那样,最后的大量数据很快进入,但是单个插入需要一些时间

单个插入可以从几千到一百万不等

我为这些方法编写的类方法如下:

def insert_keyword(self, profile_id, landing_page_id, keyword, unique_key):
    try:
        sql = '''insert into keywords_v2
                (profile_id, landing_page_id, keyword, unique_key)
                values (%s, %s, %s, %s)
                on duplicate key
                update
                landing_page_id = values(landing_page_id)'''
        self.cursor.execute(sql, (profile_id, landing_page_id, keyword, unique_key))
        self.db.commit()
        return self.cursor.lastrowid
    except Exception as e:
        self.db.rollback()
    finally:
        self.db.close()

def insert_metrics(self, data):
    try:
        sql = '''insert into keyword_metrics_v2
                (profile_id, landing_page_id, keyword_id, position, impressions, ctr, clicks, metric_dates)
                values (%s, %s, %s, %s, %s, %s, %s, %s)'''
        self.cursor.executemany(sql, data)
        self.db.commit()
        return True
    except Exception as e:
        self.db.rollback()
    finally:
        self.db.close()

如何加快速度?

一次插入100行。然后获取最后一个rowid并在第二个表中插入100行,然后计算每个rowid将是什么?获取最后一个insert_id。如果在一个查询中写入100或1000行,则100个数字按顺序排列。所以你知道每一个最后的插入id-100或1000是第1个