【发布时间】:2021-04-12 13:49:16
【问题描述】:
我正在尝试将一些数据保存在 2 个单独的表中,然后将它们与参考表连接起来。
我的数据如下所示(搜索查询和页面 URL):
[
['widget', 'www.example.com/widgets'],
['blue widget', 'www.example.com/blue-widgets'],
['red widget', 'www.example.com/widgets'],
['widget', 'www.example.com/green-widgets'],
['orange widget', 'www.example.com/widgets'],
]
如您所见,一些搜索查询被分配到多个页面,考虑到这一点以及其他一些客户端性能原因,我想将搜索查询和页面 URL 拆分到单独的表中,但是跟踪哪个搜索查询属于哪个页面网址。
为此,到目前为止,我有以下 2 个表(unique_key 列只是用于阻止添加重复项的哈希):
CREATE TABLE queries (
id BIGINT NOT NULL AUTO_INCREMENT,
query VARCHAR(400) collate utf8_bin,
unique_key varchar(100) unique,
PRIMARY KEY (id));
CREATE TABLE pages (
id BIGINT NOT NULL AUTO_INCREMENT,
page VARCHAR(2083),
unique_key varchar(100) unique,
PRIMARY KEY (id));
insert into queries (query, unique_key) values
('widget', '1234'),
('widgets', '1233'),
('blue widget', '3243'),
('red widget', '5432'),
('green widget', '4642');
insert into pages (page, unique_key) values
('www.example.com/widgets', '7895'),
('www.example.com/widgets-1', '4569'),
('www.example.com/widgets-2', '4568'),
('www.example.com/widgets-3', '1254'),
('www.example.com/widgets-4', '6527');
然后,为了构建将查询与页面相关联的数据透视表,我这样做:
CREATE TABLE page_query_join (
id BIGINT NOT NULL AUTO_INCREMENT,
query_id int,
page_id int,
PRIMARY KEY (id));
insert into page_query_join (query_id, page_id)
values ((select id from queries where query='widget' limit 1), (select id from pages where page='www.example.com/widgets' limit 1)) ON DUPLICATE KEY UPDATE query_id=query_id
这都是在 Python 脚本中完成的,我基本上创建哈希键,在一个循环中插入所有搜索查询,然后在另一个循环中插入所有页面 URL,然后最后一次循环并执行最后查询加入他们。
这个最终查找的完整函数如下所示:
def add_query_page_join(self, data):
sql = '''insert into page_query_join (query_id, page_id)
values ((select id from queries where query=%s limit 1), (select id from pages where page=%s limit 1)) ON DUPLICATE KEY UPDATE query_id=query_id'''
try:
# Execute the SQL command
self.cursor.executemany(sql, data)
# Commit your changes in the database
self.db.commit()
#return self.cursor.lastrowid
except Exception as e:
# Rollback in case there is any error
self.db.rollback()
finally:
self.db.close()
data 变量如下所示:
data = [
['widget', 'www.example.com/widgets'],
['blue widget', 'www.example.com/blue-widgets'],
['red widget', 'www.example.com/widgets'],
['widget', 'www.example.com/green-widgets'],
['orange widget', 'www.example.com/widgets'],
]
我的数据变量一次传递 1,000 个项目,但我每天总共有大约 100 万个项目要做。我当前的版本需要几个小时才能完成。
我可以做些什么来提高连接表查询的性能,或者只是我将所有内容添加到表中的方式(也许有一种方法可以一次插入所有 3 个表?)
感谢您的帮助 - 如有任何遗漏,请告诉我。
更新
解释join查询:
+----+-------------+---------+------+---------------+-------+---------+-------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+-------+---------+-------+------+----------------+
| 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
| 3 | SUBQUERY | pages | ALL | NULL | NULL | NULL | NULL | 8641 | Using where |
| 2 | SUBQUERY | queries | ref | query | query | 768 | const | 1 | Using where |
+----+-------------+---------+------+---------------+-------+---------+-------+------+----------------+
【问题讨论】:
-
评论不用于扩展讨论;这个对话是archived in chat。
标签: mysql mysql-python