简体   繁体   English

PHP-哪个更快调用mysql连接+查询或调用数据文件?

[英]php - which is faster call mysql connection + query OR call a data file?

good evening, 晚上好,
i have a mysql table that contain the users data 我有一个包含用户数据的mysql表
and i use mysql_connect, mysql_query, mysql_fetch_array to fetch user data 我使用mysql_connect, mysql_query, mysql_fetch_array来获取用户数据
this query should run on every page load and for thousands of users 此查询应在每个页面加载时运行,并针对数千个用户运行
so which is better and faster ? 那么哪个更好又更快? using mysql for every page load ? 每个页面加载使用mysql吗?
OR cache all of the user data results in a file and include it ? 或将所有用户数据结果缓存到文件中并将其包括在内?

I think the right answer is mix. 我认为正确的答案是混合。 You should cache the most common query result and retry the other "on the fly" 您应该缓存最常见的查询结果,然后“重试”另一个

If it is user data for logged in users, I would store their user info in the session rather than fetching it over and over again. 如果它是已登录用户的用户数据,我会将其用户信息存储在会话中,而不是一遍又一遍地获取它。

If it will be changing frequently and you are worried about database performance, a good option would be to cache the data using something like memcached to cache the data in memory. 如果它会经常变化并且您担心数据库性能,那么一个不错的选择是使用诸如memcached之类的数据来缓存数据到内存中。 You could request the data from cache on each request, and if user information changes, simply update the cache and the next time it is fetched it will get the new data and there is no need to hit the database unless the cache entry doesn't exist. 您可以在每次请求时从缓存中请求数据,如果用户信息发生变化,只需更新缓存,下次获取缓存时,它将获取新数据,除非缓存条目不存在,否则无需访问数据库。存在。

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

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