简体   繁体   English

在会话或cookie中存储MySQL查询结果以限制数据库调用

[英]Storing MySQL query results in session or cookies to limit DB Calls

I have a ingredient mysql database. 我有一个成分mysql数据库。 I let the user save to their profile home cabinet what ingredient per category they select. 我让用户将他们选择的每种类别的成分保存到其个人资料柜中。 I have 10 categories, so I will be making 10 queries per profile. 我有10个类别,因此每个配置文件将进行10个查询。 My query is setup like so: 我的查询是这样设置的:

select I.Iname, I.Ipic, I.IID, I.ICat 
from Ingredient I 
left outer join Cabinet C on I.IID = C.IId 
where Login.LoginID='{$_COOKIE['login']}' and I.ICategory ='fruit'; 

-- is ran 10 times since i have 10 different categories -因为我有10个不同的类别,所以跑了10次

--for each row returned it outputs divs under the specific category header. -对于返回的每一行,它在特定类别标题下输出div。

Now my question is there a more optimized way of doing this as to storing their cabinet data in session objects, arrays or using a cookie? 现在我的问题是,有一种更优化的方法来将其存储柜数据存储在会话对象,数组或使用cookie中吗? Or is running 10 of these queries not that much of a performance impact. 还是运行其中的10个查询对性能没有太大影响。 What if someone is returning 100 rows for each category? 如果有人为每个类别返回100行怎么办? Or should I just query all categories and loop through the result to place each row in its specific area. 还是我应该查询所有类别并遍历结果以将每一行放置在其特定区域中。

cabinet table : 柜子桌子:

loginID | ingredientID 
----------------------
   1    |     3 
   1    |     4 

You have 2 possible choices: 您有2种可能的选择:

  1. Leave everything as is. 一切都保持原样。 There is nothing wrong with SQL queries per se. SQL查询本身没有任何问题。 A database is not something necessary slow and hulky. 数据库不是必需的缓慢而笨拙的东西。 It is just a storage, as well as a session file (which can be in turn stored in the database, so, you'll end up in the same point but make things unnecessary complicated!). 它只是一个存储,也是一个会话文件(可以依次存储在数据库中,因此,您可能会遇到同样的问题,但会使事情变得不必要复杂!)。 Databases intended to e queried! 打算对数据库进行查询! It is the only their destination! 这是他们唯一的目的地! And they are smart enough even to cache your results. 而且它们足够聪明,甚至可以缓存您的结果。
    Note that storing mysql results in cookie is not an option at all, under any circumstances, it will slow your site for sure. 请注意,将mysql结果存储在cookie中根本不是一种选择,在任何情况下,这肯定会减慢您的站点速度。 (It seems it always needs to be clarified that there is nothing wrong with cookies as well. it is just not intended to cache data) (似乎总是需要澄清一下,Cookie也没有错。它只是不打算缓存数据)

  2. Implement some Model in terms of MVC pattern and make all data requests abstract. 根据MVC模式实施某些模型 ,并使所有数据请求抽象化。 So, you will be able to implement any caching techniques later, without altering other program code. 因此,您以后可以实现任何缓存技术,而无需更改其他程序代码。

Also note, that no performance-wise question asked on the grounds of wild guess can make sense. 还要注意,没有任何基于疯狂猜测的性能明智的问题是有意义的。
See, you were about to make even worse (using cookies), out of some grounndless assumption. 瞧,出于某些毫无根据的假设,您将变得更糟(使用Cookie)。
Always ask yourself about performance only if there is a certain problem to solve. 仅在有特定问题要解决时才询问性能。 Otherwise you will most likely just miss the point or even do things worse. 否则,您很可能会错过重点,甚至会使事情变得更糟。

Update 更新
After seeing your query I can say that there is no need to do 10 queries, - one is enough. 看到您的查询后,我可以说不需要执行10个查询,一个查询就足够了。 Just order it by ingredient and then add divs dynamically in PHP. 只需按成分排序,然后在PHP中动态添加div。 (If you have no idea how to do it, better create another question, to make things do not interfere) (如果您不知道该怎么做,最好再提出一个问题,以使事情不会干扰)

And of course you have to escape your $_COOKIE['login'] if you don't do it already. 当然,如果您还没有这样做,则必须转义 $ _COOKIE ['login']。

$cookie = mysql_real_escape_string($_COOKIE['login']);

and use that $cookie variable in the query. 并在查询中使用该$ cookie变量。

Also note that using just cookied login will make your site extremely weak in term of security - anyone will be able to login under someone else's account. 另请注意,仅使用cookie登录将使您的网站在安全性方面极其脆弱-任何人都可以使用他人的帐户登录。

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

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