简体   繁体   English

从一个表中选择数据并将另一个表连接为主“键”非常慢

[英]Selecting data from one table and joining another table for the main “key” is quite slow

I have two tables, a client would search by using a username which would search the users table for the record, and the user ID and then search through the rooms table and return the results, however my current code appears to be quite slow. 我有两个表,客户端将使用用户名进行搜索,该用户名将在用户表中搜索记录和用户ID,然后在rooms表中搜索并返回结果,但是我的当前代码似乎很慢。

I have around 750,000 records in my rooms table and around 550,000 records in my users table, so my results tend to take around 5-7 seconds to display back to the client, is there an alternative to my code below? 我的房间表中有大约750,000条记录,而用户表中有大约550,000条记录,因此我的搜索结果通常需要5到7秒的时间才能显示回客户端,下面的代码是否可以替代?

SELECT
    `rooms`.`id`,
    `rooms`.`caption`,
    `rooms`.`description`,
    `rooms`.`roomtype`,
    `rooms`.`owner`,
    `rooms`.`state`,
    `rooms`.`category`,
    `rooms`.`users_now`,
    `rooms`.`users_max`,
    `rooms`.`model_name`,
    `rooms`.`score`,
    `rooms`.`allow_pets`,
    `rooms`.`allow_pets_eat`,
    `rooms`.`room_blocking_disabled`,
    `rooms`.`allow_hidewall`,
    `rooms`.`password`,
    `rooms`.`wallpaper`,
    `rooms`.`floor`,
    `rooms`.`landscape`,
    `rooms`.`floorthick`,
    `rooms`.`wallthick`,
    `rooms`.`mute_settings`,
    `rooms`.`kick_settings`,
    `rooms`.`ban_settings`,
    `rooms`.`chat_mode`,
    `rooms`.`chat_speed`,
    `rooms`.`chat_size`,
    `rooms`.`trade_settings`,
    `rooms`.`group_id`,
    `rooms`.`tags`,
    `rooms`.`push_enabled`,
    `rooms`.`pull_enabled`,
    `rooms`.`enables_enabled`,
    `rooms`.`respect_notifications_enabled`
FROM
    `rooms`
JOIN `users` ON `users`.`id` = `rooms`.`owner`
WHERE
    `users`.`username` = 'query'
ORDER BY
    `rooms`.`users_now` DESC
LIMIT 50

Explain plan: 说明计划:

1   SIMPLE  users   const   PRIMARY,id,username username    128 const   1   Using index
1   SIMPLE  rooms   index   owner   users_now   4       50  Using where

I've indexed the appropriate columns, but still seem to get slow results. 我已经为适当的列建立了索引,但是似乎仍然得到缓慢的结果。 Thanks! 谢谢!

Your query looks fine, and the amount of records is not all that high. 您的查询看起来很好,并且记录的数量不是很高。

Make sure users.id and rooms.owner are both indexed. 确保users.id和rooms.owner都已索引。

Do you need the ORDER BY - this can cause performance issues if a filesort is required. 您是否需要ORDER BY-如果需要文件排序,可能会导致性能问题。 There are a few things you can do to improve server performance if thats the case. 如果是这种情况,您可以采取一些措施来提高服务器性能。

An EXPLAIN may help in narrowing down the issue. 解释可能有助于缩小问题范围。

As I see it, your results are based on the users table, but should contain data from teh rooms table. 如我所见,您的结果基于users表,但应包含来自rooms表的数据。 You could get your results instantly by two queries instead of one. 您可以通过两个查询而不是一个查询立即获得结果。 First get your users.id, then take that result and search the rooms table for a matching owner. 首先获取您的users.id,然后获取结果并在rooms表中搜索匹配的所有者。

select users.id from users where username='search'

Now run a whole new query for your room with the array of users.id you just got. 现在,使用刚得到的users.id数组在您的房间中运行一个全新的查询。 ForEach Code depends on the scripting language you're working in. ForEach代码取决于您使用的脚本语言。

While I admit this puts more effort on your script, it will definitely pick up your speed immediately. 尽管我承认这样做可以为您的脚本增加更多的工作量,但它肯定会立即加快您的速度。 This will still work if your users results have multiple records (ie: if there will be multiple "users.id" records returned, this method will still be snappy by comparison). 如果您的用户结果具有多个记录(即:如果将返回多个“ users.id”记录,则通过比较,此方法仍将很简单)仍然可以使用。 If there are multiple records, you can construct an array that looks like this to allow a single query to get all your rooms: 如果有多个记录,则可以构造一个看起来像这样的数组,以允许单个查询获取所有房间:

select * from rooms where owner in ('id1','id2','id3')

Another method is: 另一种方法是:

select * from rooms where owner in (select users.id from users where username='search')

This method is simpler and seems cheap, but works nicely. 这种方法比较简单,看起来很便宜,但是效果很好。 It will not return user data, just room data. 它不会返回用户数据,仅返回房间数据。 The first method would allow you to capture user data on the first query and room data on the second. 第一种方法将允许您在第一个查询上捕获用户数据,并在第二个查询上捕获房间数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个表连接到另一个表中选择最新结果 - Selecting the most recent result from one table joining to another SQL从表中选择两个值并将它们联接到另一个表中 - SQL selecting and joining two values from table into another table 连接另一个表标识的表中的数据 - Joining data from a table that is identified by another table 从另一个表中为一个主键选择多行 - Selecting multiple rows from another table for one primary key Mysql从另一个表中选择一列用作选择键后联接四个表 - Mysql Joining Four Tables After Selecting A column from another table to use as selection Key SQL - 从一个表中选择,同时在另外两个表之间进行内部连接(多对多表和另一个表) - SQL - SELECTING from one table while INNER JOINING between two others (many-to-many table and another table) 将数据从一个表复制到另一个表,并使用 laravel 中的一个按钮从主表中删除数据 - copy data from one table to another table and also delete data from main table with one button in laravel Java/Mysql 从一个表中选择主键值并将它们作为外键插入到另一个表中 - Java/Mysql Selecting Primary Key values from one table and Insert them to another table as Foreign Keys 从表中选择只连接另一个表的一行的行 - SELECT rows from a table joining only one row of another table MySQL从另一个表中的数据中选择数据 - mysql selecting data from one table based on data from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM