How to use if condition to change $table_prefix in wp_config.php

Pam,

It sounds like you are facing some major challenges, but I would highly recommend not messing with the table prefix. Doing so will lead to a series of problems, which will require hack after hack to remedy leaving you with a substantial mess of a WordPress installation.

There are some other things you can do to help solve issues with slow queries. While these things are not all necessarily straight forward, they are more worth your time than trying to hack the table prefix.

1) Make sure that you are on WP 3.4+. There were some nice improvements to standard queries in 3.4 that improve query speed.

2) For particularly problematic queries, consider setting no_found_rows to true, update_post_term_cache to false, and update_post_meta_cache to false. These can all lead to improved query performance, but you definitely need to invest some time in understanding the repercussions. Read about them in the Codex: http://codex.wordpress.org/Class_Reference/WP_Query

3) Use a persistent object cache to provide caching of queries. This is definitely a more involved process, but by caching expensive queries to RAM or disk, you can save repeated slow queries.

4) Install a page cache (WP Super Cache does this well) that will help reduce the number of times the expensive query is called. This does not directly address the query issue; however, it reduces the number of times the query may be called.

Beyond the first suggestion, I do not consider any of these to be “easy”; however, what you suggest to do would be a gigantic waste in my opinion and your time would be better spent investigating alternative options to addressing slow queries in WordPress. You will learn more about WordPress, scalability issues, and better ways to do things in a standard WordPress way.