简体   繁体   中英

Optimize Select Query from a table with Millions Rows

I am developing a website with Wordpress self-hosted 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.

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..

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 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.

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.

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. Read up on that, and use parameterised queries.

Creating an index is easy.

create index myindex on mytable ( columnname );

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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