Here is the complete query that I ended up with.
SELECT SQL_CALC_FOUND_ROWS products.product_id, products.product_code, products.product_type, products.status, products.company_id, products.list_price, products.amount, products.weight, products.tracking, products.is_edp, descr1.product as product, MIN(IF(prices.percentage_discount = 0, prices.price, prices.price - (prices.price * prices.percentage_discount)/100)) as price, companies.company as company_name, cscart_seo_names.name as seo_name
FROM cscart_products as products
LEFT JOIN cscart_product_descriptions as descr1 ON descr1.product_id = products.product_id AND descr1.lang_code = 'EN'
LEFT JOIN cscart_product_prices as prices ON prices.product_id = products.product_id AND prices.lower_limit = 1
LEFT JOIN cscart_companies AS companies ON companies.company_id = products.company_id
LEFT JOIN cscart_seo_names ON cscart_seo_names.object_id = products.product_id AND cscart_seo_names.type = 'p' AND cscart_seo_names.dispatch = '' AND cscart_seo_names.lang_code = 'EN'
LEFT JOIN cscart_product_popularity as popularity ON popularity.product_id = products.product_id
WHERE 1
GROUP BY products.product_id
ORDER BY popularity.total LIMIT 0, 200;
This query is the actual query it used to go to the products page with no searching done. The one in my OP was with an example search. But I learned the slowest query is when it returns all products. So this is the one I worked on.
The one change I had to do, following EXPLAIN's lead in mysql was on the prices join, it was using type ALL which is very bad it means it had to scan the entire table and no indexes were used.
The index I said I added before was completely redundant and it was single key. So it wasn't an indexes problem. The problem was using an IN statement:
WHERE 1 AND prices.usergroup_id IN (0)
This was edited in the fn_get_products function in fn.catalog.php you can probably do some of it with hooks though cscart doesn't have enough hooks to modify everything you need to. I've been in that spot before and once it happens once… well lets just say my installation is pretty customized by now and upgrading is a pain because of it. I did use hooks for as long as I could but somethings aren't doable by them I needed to do.
I could safely take out the usergroup condition because we don't use usergroups or any of it's features in our store. Doing this, lowered the query time. Nothing else. To half. I had been told many times before on stack overflow and other places using IN statements is a big no no and very inefficient.
So I also changed the products.status condition to be AND conditions with ('s around it and OR's on the inside this lets mysql do it's job and not slow way down.
Secondly, I changed my server's settings to use my-big.cnf suggested settings that ship with mysql this helped alot too.
Thirdly, I had before tbirnseth suggested, moved my tmp directory for mysql to the root drive. This allowed it to not be limited to 1.8 gigs for temp tables. However, I still think the best solution is leave it in the tmp partition but increase the size of the partition if you have the time to do that. I have noticed large speedups because it is no longer waiting for space to clear to finish queries.
To further make this query efficient is difficult, but I do believe getting rid of ORDER BY and GROUP BY if doable will give it another increase (as I tried, it got it down to 3.3 seconds and mysql EXPLAIN also says that it is bad especially if the order by and group by are different fields which they are here). So I basically can't do that without more time spent. The Order By clause coudl be taken out though for just a blank search for sure and shouldn't be there at all with no search query or parameters. The Group by could be taken out too maybe if the query was written in a way to not need it. I don't know if that's possible but that's where the next biggest speedups lie.
The other option I am facing is just purchasing alot of ram and turning up caching really high. When the query is cached it is .001 seconds. But it doesn't stay in memory long due to our low ram and high demand server. Ram isn't cheap for cloud hosting so my client doesn't see that as too practical at the moment.
Here is the my-big.cnf file I found I believe it ships with mysql sometimes called my-huge.cnf
I additionally used mysqltuner found here GitHub - major/MySQLTuner-perl: MySQLTuner is a script written in Perl that will assist you with your MySQL configuration and make recommendations for increased performance and stability. to help suggest adjustments to my config and used the tmpdir variable to change my directory for mysql tmp files. I adjusted as my RAM allowed but mostly stuck with the my-big.cnf settings.
```php
Example MySQL config file for very large systems.
#
This is for a large system with memory of 1G-2G where the system runs mainly
MySQL.
#
You can copy this file to
/etc/my.cnf to set global options,
mysql-data-dir/my.cnf to set server-specific options (in this
installation this directory is /var/lib/mysql) or
~/.my.cnf to set user-specific options.
#
In this file, you can use all long options that a program supports.
If you want to know which options a program supports, run the program
with the “–help” option.
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /var/lib/mysql/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 384M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
# Disable Federated by default
skip-federated
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1
# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,
# MASTER_USER=, MASTER_PASSWORD= ;
#
# where you replace , , by quoted strings and
# by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host =
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user =
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port =
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
# Point the following paths to different dedicated disks
#tmpdir = /tmp/
#log-update = /path-to-dedicated-directory/hostname
# Uncomment the following if you are using BDB tables
#bdb_cache_size = 384M
#bdb_max_lock = 100000
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /var/lib/mysql/
#innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend
#innodb_log_group_home_dir = /var/lib/mysql/
#innodb_log_arch_dir = /var/lib/mysql/
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 384M
#innodb_additional_mem_pool_size = 20M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 100M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
max_heap_table_size = 32M
tmp_table_size = 64M
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[isamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
```