简体   繁体   English

从具有百万行的表中优化选择查询

[英]Optimize Select Query from a table with Millions Rows

I am developing a website with Wordpress self-hosted CMS. 我正在使用Wordpress自托管CMS开发一个网站。

In one of the page, i ran a function that do a query into wordpress database, to check wether a post is already posted or not, i am comparing the title to check it. 在其中一个页面中,我运行了一个函数,对wordpress数据库进行查询,检查是否已发布帖子,我正在比较标题以进行检查。

Here is my query: 这是我的查询:

$wpdb->get_row("SELECT id FROM wp_posts WHERE post_title = '" . $title . "'", 'ARRAY_A');

So i am checking whether $title is posted or not, but i am afraid if the number of post grows, let says 1 Million Posts, i am afraid that it will be very slow.. 所以我正在检查是否发布了$ title,但我担心如果帖子的数量增加,就说100万帖子,我担心它会很慢......

Any suggestion on how to make this query faster? 有关如何更快地进行此查询的任何建议? i heard about CREATE INDEX and mysql caching but i don't understand how to implement it.. any explanations and references suggestion will be highly appreciated. 我听说过CREATE INDEX和mysql缓存但我不明白如何实现它...任何解释和参考建议将受到高度赞赏。

create indexes on your tables based on most common columns that are used in querying data, such as here where you are looking for the post_title. 根据查询数据时使用的最常见列创建表的索引,例如在此处查找post_title。

Additionally, from you building the SQL-Select statement on the fly like you are, you are wide-open for SQL-Injection attacks and should escape out the string and preferrably do with parameterized query calls. 此外,您可以像现在一样动态构建SQL-Select语句,您可以对SQL注入攻击进行全面开放,并且应该从字符串中转义,最好使用参数化查询调用。

Try this: 尝试这个:

CREATE INDEX IX_wp_posts_post_title ON wp_posts (post_title)

The creation of the index will take a long time but afterward your queries should be close to instant. 索引的创建需要很长时间,但之后您的查询应该接近即时。

Not quite sure what you are trying to achieve here. 不太清楚你想在这里实现什么。 It doesn't seem like the end of the world to have two posts with the same title. 拥有相同标题的两个帖子似乎不是世界末日。

More concerning though is your code is totally sql-injectable. 更令人担忧的是你的代码完全是sql-injectable。 Read up on that, and use parameterised queries. 阅读,并使用参数化查询。

Creating an index is easy. 创建索引很容易。

create index myindex on mytable ( columnname ); 在mytable(columnname)上创建索引myindex;

This will help selects... but if you are really having millions of rows, you might be better to get some proper database advice - you may need to partition your data. 这将有助于选择...但是如果你真的有数百万行,你可能最好得到一些适当的数据库建议 - 你可能需要对数据进行分区。

If you are checking to see if a particular post is already in your database, you should be using the post's id to test instead of its title. 如果您要检查数据库中是否已存在特定帖子,则应使用帖子的ID来测试而不是标题。 One because it is a garanteed unique identifier (assuming it is the primary key), and two because the query will be able to search for it far far faster. 一个是因为它是一个保证的唯一标识符(假设它是主键),两个因为查询将能够以更快的速度搜索它。

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

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