简体   繁体   English

需要帮助的mysql连接

[英]need help with mysql joins

Users table
 user_id
 pic_url
 name

friends table
 auto_id
 userid
 friendid
 status

actions table
 auto_id
 userid
 type
 subject
 body
 datetime

I want to make a friend stream of updates thats shows updates, could be a blog post, status change, anything but should only show the ones that are from a friend of the logged in user 我想让更新的朋友流显示更新,可以是博客文章,状态更改等,但只能显示来自已登录用户的朋友的更新

Here is what I came up with but my userbase is very large so performance is a must, is there a better way of doing this? 这是我想出的,但是我的用户群很大,因此性能是必须的,是否有更好的方法呢? Please show me 请给我看

SELECT u.user_id, u.pic_url, u.name, a.auto_id, a.userid, a.type, a.subject, a.body, a.datetime
FROM actions AS a
LEFT JOIN users AS u ON u.auto_id=a.userid
LEFT JOIN friends AS f ON f.userid=a.userid
WHERE f.friendid=1 //1 would be my user ID 
AND f.status=active

Please help I don't think this is correct. 请帮助我,我认为这是不正确的。

Lets say there is 50,000 users and my userid is #1 and I am friends with 20,000 users, it should return all entries in the actions table that is published by a user I am friends with, also need to modify to include actions from myself 可以说有50,000个用户,我的用户ID是#1,我是20,000个用户的朋友,它应该返回由我作为朋友的用户发布的动作表中的所有条目,还需要修改以包含我自己的动作

I have heard some people talki about using some sort of hash table for faster lookups would something like that be possible here? 我听说有人谈论使用某种哈希表进行更快的查找,这里可能会发生这种情况吗?

Thanks for any help 谢谢你的帮助

I have heard some people talki about using some sort of hash table for faster lookups would something like that be possible here? 我听说有人谈论使用某种哈希表进行更快的查找,这里可能会发生这种情况吗?

It's called an index , and you should add one to each column that you're planning on using a JOIN (or matching with an explicit constraint like >, >=, =, <=, < or an IN () clause which matches only items in a stated list). 这称为索引 ,您应该使用JOIN(或与显式约束(例如>, >=, =, <=, <或仅匹配IN ()IN ()子句匹配IN ()在要计划的每一列上添加一个索引说明清单中的项目)。 This way the database server can jump right to the correct entries in the index, rather than having to do a brute force search through all of the table rows. 这样,数据库服务器可以直接跳到索引中的正确条目,而不必在所有表行中进行蛮力搜索。 It's exactly like an index in a book. 就像书中的索引一样。 If you wanted to find the pages in a book in which the name "Knuth" appears, you have two choices. 如果要查找名称为“ Knuth”的书中的页面,则有两种选择。 If the book has an index, you look in the index and hope the name is there. 如果这本书有索引,您可以在索引中查找并希望其中有名称。 If the book does not have an index, you'll just have to read through the whole thing yourself, and that will take much longer. 如果这本书没有索引,那么您只需要自己阅读整个内容,这将花费更长的时间。

If you care about ordering/sorting (or doing any kind of relative numeric/string comparison), it should be a sorted index. 如果您关心排序/排序(或进行任何形式的相对数字/字符串比较),则它应该是排序索引。 Otherwise it can be a hashtable index, which is faster for tables with lots of rows, but carries no sorting information. 否则,它可以是哈希表索引,这对于具有很多行的表来说更快,但是没有排序信息。 These types of details are likely to have different syntaxes/options depending on which type of database server software is used.** (see note below) 这些类型的详细信息可能使用不同的语法/选项,具体取决于所使用的数据库服务器软件的类型。**(请参阅下面的注释)

Note that primary keys already have an automatically generated index, so you don't have to add one yourself. 请注意,主键已经具有自动生成的索引,因此您不必自己添加一个。 Note also that if you have a multiple-column primary key, eg (State, City, Zipcode) then there will effectively be indices on the leftmost subsets of the primary key, eg you get an index on State, and (State,City), and (State,City,Zipcode) for free, but if you want to JOIN on Zipcode or City or (City,Zipcode) then you need to create your own indices in addition to those provided by the primary key. 还请注意,如果您有一个多列主键,例如(州,城市,邮政编码),则主键的最左侧子集将有效地存在索引,例如,您获得了State的索引,而(State,City) ,和(State,City,Zipcode)是免费的,但是如果您想在Zipcode或City或(City,Zipcode)上加入JOIN,则除了主键提供的索引之外,还需要创建自己的索引。

In your case, it looks like you should have indices on these columns (I've *-ed the columns I am assuming are already primary keys). 在您的情况下,您似乎应该在这些列上有索引(我*-我假定的列已经是主键)。 Unless you have any significance on the numerical order of your user IDs, those would be good candidates for hashtable indices. 除非您对用户ID的数字顺序有任何意义,否则这些将是哈希表索引的良好候选者。

Users.user_id*
Friends.user_id
Friends.friend_id
Friends.active
Actions.user_id

**For MySQL, you add a clause to the CREATE INDEX statement that says USING HASH for a hashtable index, or USING BTREE (for a sorted index)... ignore RTREEs as those are for spatial data. **对于MySQL,您在CREATE INDEX语句中添加了一个子句,该子句表示将USING HASH用于哈希表索引,或将USING BTREE(用于排序索引)...忽略RTREE,因为它们是空间数据。 Note also that MySQL doesn't allow HASH indices on the common storage engines InnoDB and MyISAM. 还要注意,MySQL不允许在通用存储引擎InnoDB和MyISAM上使用HASH索引。 Really large datasets that need high-performance probably need to have data mirrored on an in-memory table with a HASH index. 真正需要高性能的大型数据集可能需要将数据镜像到带有HASH索引的内存表中。 With 50,000 rows you probably don't need to worry about it; 拥有50,​​000行,您可能不必担心它。 a BTREE's search time is O(log n) whereas HASH is O(1) and there's probably not that much difference. BTREE的搜索时间为O(log n),而HASH的搜索时间为O(1),并且差别可能不大。 BTREEs are very wide and are designed not to be deep; BTREE非常宽,并且设计得不深; to require a single additional comparison in the search step, you may need to increase the # of rows by a factor of 10 or 100. 要在搜索步骤中进行一次单独的比较,您可能需要将行数增加10或100。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM