简体   繁体   English

MySQL的显示表/列-性能问题

[英]mysql show table / columns - performance question

I'm working on a basic php/mysql CMS and have a few questions regarding performance. 我正在使用基本的php / mysql CMS,并且对性能有一些疑问。

When viewing a blog page (or other sortable data) from the front-end, I want to allow a simple 'sort' variable to be added to the querystring, allowing posts to be sorted by any column. 从前端查看博客页面(或其他可排序数据)时,我想允许将简单的“ sort”变量添加到查询字符串中,从而允许按任何列对帖子进行排序。 Obviously I can't accept anything from the querystring, and need to make sure the column exists on the table. 显然,我不能接受来自查询字符串的任何内容 ,并且需要确保该列存在于表中。

At the moment I'm using 目前我正在使用

SHOW TABLES;

to get a list of all of the tables in the database, then looping the array of table names and performing 获取数据库中所有表的列表,然后循环遍历表名数组并执行

SHOW COLUMNS;

on each. 在每一个上。

My worry is that my CMS might take a performance hit here. 我担心我的CMS可能会在此处降低性能。 I thought about using a static array of the table names but need to keep this flexible as I'm implementing a plugin system. 我考虑过使用表名的静态数组,但是在实现插件系统时需要保持灵活。

Does anybody have any suggestions on how I can keep this more concise? 有人对我如何保持简洁有任何建议吗?

Thankyou 谢谢

If you using mysql 5+ then you'll find database information_schema usefull for your task. 如果您使用mysql 5+,那么您会发现数据库information_schema对您的任务很有用。 In this database you can access information of tables, columns, references by simple SQL queries. 在此数据库中,您可以通过简单的SQL查询访问表,列,引用的信息。 For example you can find if there is specific column at the table: 例如,您可以找到表格中是否存在特定列:

SELECT count(*) from COLUMNS 
WHERE 
    TABLE_SCHEMA='your_database_name' AND
    TABLE_NAME='your_table' AND
    COLUMN_NAME='your_column';

Here is list of tables with specific column exists: 这是存在特定列的表的列表:

SELECT TABLE_SCHEMA, TABLE_NAME from COLUMNS WHERE COLUMN_NAME='your_column';

Since you're currently hitting the db twice before you do your actual query, you might want to consider just wrapping the actual query in a try{} block. 由于当前在执行实际查询之前要两次访问数据库,因此您可能需要考虑将实际查询包装在try{}块中。 Then if the query works you've only done one operation instead of 3. And if the query fails, you've still only wasted one query instead of potentially two. 然后,如果查询有效,则只执行一项操作,而不是3。并且如果查询失败,您仍然仅浪费了一个查询,而不是可能的两个查询。

The important caveat (as usual!) is that any user input be cleaned before doing this. 重要的警告(照常!)是在执行此操作之前,请先清除所有用户输入。

You could query the table up front and store the columns in a cache layer (ie memcache or APC). 您可以先查询表,然后将列存储在缓存层(即memcache或APC)中。 You could then set the expire time on the file to infinite and only delete and re-create the cache file when a plugin has been newly added, updated, etc. 然后,您可以将文件的到期时间设置为无限,并且仅在新添加,更新了插件等之后才删除并重新创建缓存文件。

I guess the best bet is to put all that stuff ur getting from Show tables etc in a file already and just include it, instead of running that every time. 我猜最好的选择是将您从“显示表”等获得的所有内容都放在一个文件中,然后仅包含它,而不是每次都运行它。 Or implement some sort of caching if the project is still in development and u think the fields will change. 如果项目仍在开发中,或者您认为领域会发生变化,请实施某种缓存。

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

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