我有 4 个不同的表,它们为用户存储不同的事务,我想为某些特定用户合并所有事务。但问题是,所有这 4 个表都有大量数据,因此当我尝试将它们全部合并并与用户连接时,需要花费数小时来计算。我正在做的是:
SELECT blablabla
FROM transactions1 t1
JOIN users ON (users.id = t1.user_id AND user.state = 1 AND user.friends > 1 AND user.loc = 'Turkey')
UNION
SELECT blablabla
FROM transactions2 t2
JOIN users ON (users.id = t2.user_id AND user.state = 1 AND user.friends > 1 AND user.loc = 'Turkey')
UNION
SELECT blablabla
FROM transactions3 t3
JOIN users ON (users.id = t3.user_id AND user.state = 1 AND user.friends > 1 AND user.loc = 'Turkey')
UNION
SELECT blablabla
FROM transactions4 t4
JOIN users ON (users.id = t4.user_id AND user.state = 1 AND user.friends > 1 AND user.loc = 'Turkey')
ORDER BY date DESC
问题是,我
JOIN users ON (users.id = t1.user_id AND user.state = 1 AND user.friends > 1 AND user.loc = 'Turkey')
通过将每个事务表与用户连接来运行此过滤器 4 次。
为了获得最佳实践并提高查询效率,我应该如何改进我的查询?
谢谢 !