Mysql VIEWS 与 PHP 查询
我正在开发一个 Web 应用程序,该应用程序涉及在各种列表中创建餐厅列表,例如Joe 必须访问的地方".现在对于每个餐厅和列表,我在网站上显示了计算
I am working on a web application which involves create list of Restaurants in various lists like "Joe's must visit places". Now for each Restaurant and list, I have display on website which calculates
- 计算餐厅的受欢迎程度
- 列表的受欢迎程度
- 餐厅所在的列表数量
目前我正在 PHP 中使用 MySQL 语句,但计划切换到 MySQL VIEWS 并在 PHP 中执行一个简单的选择语句...
Currently I am using MySQL statements in PHP for this but planning to switch to MySQL VIEWS and do a simple select statement in PHP...
我的问题是,在 PHP 中使用 VIEWS 编写 sql 查询的优点/缺点是什么?
使用视图增加了抽象级别:您以后可以更改表的结构,而不必更改显示有关列表信息的代码,因为您仍将查询视图(尽管视图定义可能会更改).
Using views adds a level of abstraction : you may later change the structure of your tables, and you will not have to change the code that displays the information about the lists, because you will still be querying the view (the view definition may change, though).
主要区别在于每次插入后都会更新视图,这样每当您查询视图时数据就准备好",而使用自定义查询将使 MySQL 每次都计算所有内容(当然有一些缓存).
The main difference is that views are updated after each insertion, such that the data is "ready" whenever you query the view, whereas using your custom query will have MySQL compute everything each time (there is some caching, of course).
最重要的是,如果您的列表更新频率低于查看频率,您会发现使用视图的性能有所提升.
The bottom line is that if your lists are updated less frenquently than they are viewed, you will see some gains in performance in using views.