MySQL:使用视图从多个表中查询数据
我想为简单的搜索创建一个查询结果页面,但我不知道,我是否应该在数据库中使用视图,如果我使用与创建查询相同的语法将查询写入代码中会更好吗?看法。
当我想为我的网站构建一个搜索模块(拥有大量用户和页面加载量)时,合并 7 个表的更好解决方案是什么? (我正在同时搜索更多表)
I want to create a query result page for a simple search, and i don't know , should i use views in my db, would it be better if i would write a query into my code with the same syntax like i would create my view.
What is the better solution for merging 7 tables, when i want to build a search module for my site witch has lots of users and pageloads?
(I'm searching in more tables at the same time)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您最好使用带有连接的普通查询,而不是视图。 MySQL 中的视图未优化。确保您的表在连接中使用的字段上正确建立了索引
you would be better off using a plain query with joins, instead of a view. VIEWS in MySQL are not optimized. be sure to have your tables properly indexed on the fields being used in the joins
如果你总是使用所有 7 个表,我认为你应该使用视图。请注意,mysql 在创建视图时会更改您的原始查询,因此将查询保存在其他位置始终是一个好习惯。
另外,请记住您可以调整 mysql 的查询缓存环境变量,以便它存储更多数据,从而使您的查询响应更快。但是,我建议您使用其他方法进行缓存,例如 memcached。付费版本的mysql原生支持memcached,但我确信你可以在应用层实现它,没有问题。
祝你好运!
If you always use all 7 tables, i think you should use views. Be aware that mysql changes your original query when creating the view so its always good practice to save your query elsewhere.
Also, remember you can tweak mysql's query cache env var so that it stores more data, therefore making your queries respond faster. However, I would suggest that you used some other method for caching like memcached. The paying version of mysql supports memcached natively, but Im sure you can implement it in the application layer no problem.
Good luck!