简体   繁体   English

使用php和mysql构建网站的更有效方法(已取消或已查询?)

[英]More efficient way to contruct a website using php and mysql (cashed or queried?)

Background: 背景:

I've just started learning php to create a website to display user data from a betting program held in a mysql database. 我刚刚开始学习php,以创建一个网站来显示来自mysql数据库中的投注程序的用户数据。

The program in question (An IRC bet bot client written in python) originally used .csv text file arrays to hold user data. 有问题的程序(用python编写的IRC bet bot客户端)最初使用.csv文本文件数组来保存用户数据。 However I realized that this method of data storage really wasn't scale-able and held a lot of data in memory (thus required lots of file writes and flat text file searches). 但是我意识到这种数据存储方法实际上是不可扩展的,并且在内存中保存了大量数据(因此需要大量文件写入和纯文本文件搜索)。

I re-implemented the program to use a mySQL database instead. 我重新实现了使用MySQL数据库的程序。 I'm currently re-writing the website. 我目前正在重写网站。

Is it best that for every user I query the mySQL database once for each variable stored (thus hundreds of queries need to construct a user list page)? 对于每个用户,我为存储的每个变量查询一次mySQL数据库是否最好(因此数百个查询需要构建一个用户列表页面)? Or should I query the database once but the data into an array and cherry pick the variables out of that array that I need to display? 还是我应该查询一次数据库,但是将数据放入一个数组中,然后从该数组中挑选出我需要显示的变量? Thus only querying once but holding more data in memory and doing much more server based processing. 因此,仅查询一次,但将更多数据保存在内存中,并进行更多基于服务器的处理。

The original website (That still works using text.csv files) can currently be found here: http://xcoins.co.uk (However this will soon be updated with the mysql based version (hopefully)) 当前可以在以下位置找到原始网站(仍可以使用text.csv文件运行): http : //xcoins.co.uk (不过,很快将使用基于mysql的版本进行更新(希望如此))

Question: 题:

Is it better to query a mySQL database once per user get lots of data and sort it using php to display a list of users sorted by a value (some of the returned data from the query may be discarded or maybe cached). 每个用户获得大量数据后查询mySQL数据库是否更好,并使用php对其进行排序,以显示按值排序的用户列表(查询返回的某些数据可能会被丢弃或缓存)。 OR should I query the database lots of times for each variable of user data I need on a page but thus it will only grab what data is needed (but would require around 100 queries to be sent an received to display a single user list page)? 或者我应该在页面上为我需要的用户数据的每个变量多次查询数据库,但是这样它只会获取需要的数据(但是将需要发送大约100条查询才能显示单个用户列表页面) ?

How many queries are generally acceptable to display a single webpage with lots of data on it? 显示一个包含大量数据的网页通常可以接受多少查询?

The common way is to retrieve as much information as you need for a given page (but no more), and to use this result set to populate the page. 常见的方法是检索给定页面所需的尽可能多的信息(但不要再获取更多信息),并使用此结果集填充页面。

So, to create the "customer profile" page, you might run a query like: 因此,要创建“客户资料”页面,您可以运行如下查询:

select p.name, 
       p.address, 
       sum(t.amount) as balance
from   profile p, 
       transactions t
where  p.id = ?
and    p.id = t.id

(Apologies for archaic join syntax - old habits die hard). (过时的联接语法道歉-旧习惯很难解决)。

This would give you a PHP result set, which you'd print to the page in the appropriate place. 这将为您提供一个PHP结果集,您可以将其打印到适当位置的页面上。

Of course, often you cannot retrieve all the data you need in a single query, either because there isn't a natural join (eg showing the number of active users on the system), or for performance/complexity reasons. 当然,由于没有自然的连接(例如,显示系统上活动用户的数量)或出于性能/复杂性的原因,通常您无法在单个查询中检索所需的所有数据。 In this case, you'd write individual queries for those data elements. 在这种情况下,您将为这些数据元素编写单个查询。

You may want to look at PHP frameworks which abstract away a lot of this stuff for you. 您可能需要查看PHP框架,该框架为您提供了很多这些东西。

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

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