[英]mysql select cache optimization
Having a table called 'books', then multiple tables with a primary key, and value: EG 'library', and 'genre'. 有一个名为“ books”的表,然后有多个带有主键和值的表:例如EG“ library”和“ genre”。 Then this primary key is stored in the 'books' table for every book.
然后,该主键将存储在每本书籍的“书籍”表中。
What's an efficient way to retrieve all books in library A? 什么是检索库A中所有书籍的有效方法?
Some approaches: 一些方法:
select books where library = (select id where library = 'A')
- mysql caches the sub query? select books where library = (select id where library = 'A')
-mysql缓存子查询? What's the reccomended (and fast) way of doing this? 推荐的(快速)方法是什么?
Use a relational model between libraries and books and query it using joins. 在图书馆和书籍之间使用关系模型,并使用联接对其进行查询。 This is fairly fast, as long as you have the primary keys and indexes for
library_id
set up. 只要您设置了
library_id
的主键和索引,这就相当快。
table: libraries
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | PRI | NULL | |
| name | varchar(100) | NO | | NULL | |
+-------+--------------+------+-----+---------+-------+
+------+------+
| id | name |
+------+------+
| 1 | Foo |
| 2 | Bar |
+------+------+
table: books
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | 0 | |
| library_id | int(11) | NO | MUL | NULL | |
| author | varchar(100) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
+------------+--------------+------+-----+---------+-------+
+----+------------+--------+------+
| id | library_id | author | name |
+----+------------+--------+------+
| 1 | 1 | Jon | Baz |
| 2 | 1 | Bill | Baz |
| 3 | 2 | Mary | Abc |
+----+------------+--------+------+
This allows for very easy querying. 这使得查询非常容易。 To find all books in the library with the name of 'Foo', you'd use a query like this:
要在图书馆中查找所有名称为“ Foo”的书籍,请使用类似以下的查询:
mysql> SELECT books.* FROM books
-> JOIN libraries ON libraries.id = books.library_id
-> AND libraries.name = 'Foo';
+----+------------+--------+------+
| id | library_id | author | name |
+----+------------+--------+------+
| 1 | 1 | Jon | Baz |
| 2 | 1 | Bill | Baz |
+----+------------+--------+------+
If you already know the library ID, you don't even need a join: 如果您已经知道库ID,那么您甚至不需要加入:
mysql> SELECT * FROM books
-> WHERE library_id = 2;
+----+------------+--------+------+
| id | library_id | author | name |
+----+------------+--------+------+
| 3 | 2 | Mary | Abc |
+----+------------+--------+------+
If you want performance on a relation database you must use Foreign Keys. 如果要提高关系数据库的性能,则必须使用外键。 Indexes are fast but FK's are faster simply because they 'know' what on the other end.
索引速度很快,但是FK速度更快,这仅仅是因为它们“知道”另一端的内容。 If you are still having performance issues after that you must first understand how the DB engine handles your request.
如果之后仍然遇到性能问题,则必须首先了解数据库引擎如何处理您的请求。 If you have a lot of books and only a few libraries make sure the engine selects your library first;
如果您有很多书并且只有几个图书馆,请确保引擎首先选择您的图书馆;否则,请执行以下步骤。 not 'books inner join library' but 'library inner join books'.
不是“书籍内部连接库”,而是“图书馆内部连接书”。 If you don't use data from both tables you must go for subqueries, 'cause that way the engine will only use the keys from the subqueries for the join, instead of the complete table.
如果您不使用两个表中的数据,则必须使用子查询,因为这样引擎将仅使用子查询中的键进行连接,而不使用完整表。 Never use your (PHP)-code to cache database results for the next query.
永远不要使用(PHP)代码为下一个查询缓存数据库结果。 If things are getting to complex to get done in one go use temporary table.
如果事情变得复杂起来,请使用临时表。
I would go for : 我会去:
books
and library
books
和library
之间的内在联系 After all, that's the most logical thing to do, in this kind of situation, considering : 毕竟,在这种情况下,考虑以下几点是最合乎逻辑的:
If you want to optimize things a little, yes, you could use your last point ; 如果您想稍微优化一些东西,是的,您可以使用最后一点; but not sure that would change much actually :
但不确定实际上会发生很大变化:
If I were to add some cache, I would probably cache the result of the whole book-search. 如果要添加一些缓存,则可能会缓存整个图书搜索的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.