Reputation: 1845
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.
Upvotes: 3
Views: 1293
Reputation: 19251
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.
Upvotes: 0
Reputation: 2197
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.
Upvotes: 0
Reputation: 838166
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.
Upvotes: 1
Reputation: 48139
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.
Upvotes: 1