Note that there are some explanatory texts on larger screens.

plurals
  1. POMySql single table optimization options
    primarykey
    data
    text
    <p>I have a table with around 6 million rows and has many columns with lot of data.</p> <p>I want to implement a search functionality on this table mostly based on date range when the row was created and some other columns.</p> <p>No other table is required so no question of joins in query. Also, I have implemented indexes on date column and other relevant columns.</p> <p>Still the search takes long time if I search for large ranges, considering number of rows the table has. I have increased key_buffer_size to 1G and also mysql_query_cache to 500M.</p> <p>I want to know if,</p> <ol> <li>There is anything else I can do, optimizing any other variables that may increase performance.</li> <li>Should I look into re-designing the table itself?</li> </ol> <p>I want to explore all options before increasing RAM or CPU.</p> <p><strong>UPDATE</strong>:</p> <p>Below are more details</p> <p>CPU: Intel Quadcore RAM: 4GB OS: CentOS</p> <pre><code>+-----------------------------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------------------+---------------+------+-----+---------+----------------+ | deal_id | int(11) | NO | PRI | NULL | auto_increment | | deal_group_id | int(11) | NO | PRI | NULL | | | site_id | int(11) | YES | MUL | NULL | | | datetime | char(14) | YES | MUL | NULL | | | deal_category | varchar(200) | YES | MUL | NULL | | | vendor_name | varchar(200) | YES | | NULL | | | area | varchar(200) | YES | MUL | NULL | | | address | text | YES | | NULL | | | country_code | varchar(5) | NO | MUL | NULL | | | expired | int(1) | NO | | 0 | | | type | varchar(20) | NO | | REGULAR | | | pn_id | int(11) | NO | | 0 | | +-----------------------------+---------------+------+-----+---------+----------------+ </code></pre> <p>Above is the schema. In the where clause the combination of <code>datetime</code> in range, <code>deal_category</code>, <code>vendor_name</code> in like and <code>country_code</code> is used. All are indexed. Below is one sample query</p> <p><code> SELECT distinct t.deal_id, t.deal_group_id, t.site_name, UNIX_TIMESTAMP(t.datetime) as datetime, t.deal_category as deal_category, t.vendor_name, t.area as area, t.country_code FROM table t WHERE 1 AND t.country_code = 'US' AND datetime BETWEEN '20110601' AND '20120303' limit 10;</code></p> <p>I have increased <code>innodb_buffer_pool_size</code> to around 75%, it has improved the performance for some queries, but still takes lot of time if all filters are included.</p> <p>Below is the my.cnf file settings,</p> <p><code>query_cache_size=500M</code></p> <p><code>max_allowed_packet= 1G</code></p> <p><code>innodb_buffer_pool_size = 3G</code></p> <p><code>key_buffer_size = 1G</code></p> <p>This table is quite busy, there are hundreds of writes happening every minute.</p> <p>Hope this gives clearer picture.</p> <p><strong>More Updates</strong></p> <p>Below is the explain output and indexes on table,</p> <pre><code>+----+-------------+-------+------+-----------------------+--------------+---------+-------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+-----------------------+--------------+---------+-------+--------+-------------+ | 1 | SIMPLE | t | ref | country_code,datetime | country_code | 17 | const | 521556 | Using where | +----+-------------+-------+------+-----------------------+--------------+---------+-------+--------+-------------+ </code></pre> <p>Indexes:</p> <pre><code>+-------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ | table | 0 | PRIMARY | 1 | deal_id | A | 5810775 | NULL | NULL | | BTREE | | | table | 0 | PRIMARY | 2 | deal_group_id | A | 5810775 | NULL | NULL | | BTREE | | | table | 1 | site_id | 1 | site_id | A | 2109 | NULL | NULL | YES | BTREE | | | table | 1 | area | 1 | area | A | 18927 | NULL | NULL | YES | BTREE | | | table | 1 | country_code | 1 | country_code | A | 20 | NULL | NULL | | BTREE | | | table | 1 | deal_group_id | 1 | deal_group_id | A | 1936925 | NULL | NULL | | BTREE | | | table | 1 | deal_category | 1 | deal_category | A | 20 | NULL | NULL | YES | BTREE | | | table | 1 | datetime | 1 | datetime | A | 5810775 | NULL | NULL | YES | BTREE | | +-------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload