horizontal splitting? though i guess 1.2 million records are not that much to introduce horizontal splitting.. try to locate the bottom neck... also the problem may lie with your hardware as well for example harddisk almost full etc.
I suspect the assertion "whenever we query this table mysql hangs down" is an overbid -- for example, with MyISAM, SELECT COUNT(*) FROM TheTable
should be very fast, essentially "no matter what". Sure, some queries will be slow -- especially if the table is not indexed properly for the queries, or if MySQL's alleged optimizer is picking the wrong strategy (but you could give it hints).
Why don't you show us the CREATE TABLE
(including indices), a couple of the queries that take too long, ideally a precise measure of how long they take, and the output of EXPLAIN SELECT
(&c) for those couple queries -- I bet we could really be of some help then!
Edit: the CREATE TABLE
essentially shows that the table is just too "broad" -- far too many columns -- to expect decent performance (even though no queries were shown). The schema needs a redesign, breaking up chunks of this huge monolithic table (e.g., the address-related information) into other auxiliary tables. Exactly how to best do it depends entirely on the queries that are most important to optimize, so, not knowing the queries in question, I'm not even going to attempt the task.
Edit again: so the query has been posted and uses other tables, accounts
and account_contacts
, as well as the hugely broad contacts
one described; the query as posted (trying to make sense of it by formatting &c) is:
SELECT acc.id, acc.name, con_reports_to.first_name, con_reports_to.last_name
FROM contacts
LEFT JOIN accounts_contacts a_c
ON a_c.contact_id = '9802f40d-78bb-8dd4-dfaa-43f1064ccd5e' AND
a_c.deleted=0
LEFT JOIN accounts acc
ON a_c.account_id = acc.id AND
acc.deleted=0
LEFT JOIN contacts con_reports_to
ON con_reports_to.id = contacts.reports_to_id
WHERE contacts.id = '9802f40d-78bb-8dd4-dfaa-43f1064ccd5e'
Why the LEFT JOIN
s here instead of normal INNER
joins? Is it possible in each case that there's no corresponding row on the right-hand-side table? For example, if there's no line in a_c
with the given values for contact_id
and deleted
, then all the fields of a_c
in the first LEFT JOIN
will be NULL
, so there can be no correspondence for acc
either: is it important to emit NULL, NULL
as the first two columns in this case? Moreover the JOIN
conditions for a_c
an acc
make no reference at all to contacts
, so this will be a cartesian product: every line selected from acc
, if any, will pair up with every line selected from con_reports_to
. So the a_c
/acc
query could be entirely separated from the one on contacts
and con_reports
, presumably ligthtening the query considerably (the two logically separate results could of course easily be put together again in the client).
What does EXPLAIN SELECT
say for this complex query and what does it say for the two lighter-weight separate ones I'm suggesting? What indices are on the accounts
and account_contact
tables?