Reputation: 4337
ive run into a bit of a snag here, it seems that my pages are loading slower and slower the most content is being added to the database.
i have already added indexes on the ID and ID_* fields, that fixed the problem a while ago, but is not working anymore.
i've also run OPTIMIZE on all tables.
here is my query:
select
l.id, l.id_infusionsoft, l.name_first, l.name_last, l.postcode, a.website as a_website, l.website as l_website, l.date_added,
a.id_dealership, a.id_lead, a.id as id_assign, a.date_assigned, a.manual_or_auto, a.assigned_by,
d.name as dealership,
COALESCE(a.date_assigned, l.date_added) AS date_sort
from `leads` as l
left join `assignments` as a on (a.id_lead = l.id)
left join `dealerships` as d on (d.id = a.id_dealership)
order by date_sort desc
here are the table structures:
CREATE TABLE assignments (
id int(11) NOT NULL auto_increment,
id_dealership int(11) NOT NULL,
id_lead int(11) NOT NULL,
date_assigned int(11) NOT NULL,
website varchar(255) NOT NULL default '',
make varchar(255) NOT NULL default '',
model varchar(255) NOT NULL default '',
ip_address varchar(255) NOT NULL default '',
is_reassign varchar(255) NOT NULL default 'no',
manual_or_auto varchar(255) NOT NULL default 'N/A',
assigned_by varchar(255) NOT NULL default 'N/A',
PRIMARY KEY (id),
KEY id_dealership (id_dealership),
KEY id_lead (id_lead),
KEY date_assigned (date_assigned)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=24569 ;
CREATE TABLE dealerships (
id int(11) NOT NULL auto_increment,
province varchar(255) NOT NULL default '',
city varchar(255) NOT NULL default '',
`name` varchar(255) NOT NULL,
email text NOT NULL,
email_subject varchar(255) NOT NULL default 'Car Lead',
`type` varchar(255) NOT NULL,
make varchar(255) NOT NULL,
leads int(11) NOT NULL default '0',
`status` varchar(255) NOT NULL,
low_notif int(11) NOT NULL,
reassign_id int(11) NOT NULL,
reassign_days int(11) NOT NULL,
salesman varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=176 ;
CREATE TABLE leads (
id int(11) NOT NULL auto_increment,
id_infusionsoft int(11) NOT NULL,
name_first varchar(255) NOT NULL,
name_last varchar(255) NOT NULL,
email varchar(255) NOT NULL,
phone_home varchar(255) NOT NULL,
phone_cell varchar(255) NOT NULL,
phone_work varchar(255) NOT NULL,
postcode varchar(255) NOT NULL,
website varchar(255) NOT NULL,
address varchar(255) NOT NULL,
province varchar(255) NOT NULL,
employer varchar(255) NOT NULL,
city varchar(255) NOT NULL,
rentorown varchar(255) NOT NULL,
emp_months varchar(255) NOT NULL,
emp_years varchar(255) NOT NULL,
sin varchar(255) NOT NULL,
occupation varchar(255) NOT NULL,
monthly_income varchar(255) NOT NULL,
bankruptcy varchar(255) NOT NULL,
tradein varchar(255) NOT NULL,
cosign varchar(255) NOT NULL,
monthly_payment varchar(255) NOT NULL,
residence_years varchar(255) NOT NULL,
residence_months varchar(255) NOT NULL,
birthday varchar(255) NOT NULL,
make varchar(255) NOT NULL,
model varchar(255) NOT NULL,
date_added int(11) NOT NULL,
ip_address varchar(255) NOT NULL default '',
time_to_call varchar(255) NOT NULL,
referrer varchar(255) NOT NULL default '',
`source` varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY id_infusionsoft (id_infusionsoft),
KEY date_added (date_added)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=20905 ;
what can i do to make my query run faster?
EDIT: here is the explain select output:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE l ALL NULL NULL NULL NULL 15381 Using temporary; Using filesort
1 SIMPLE a ref id_lead id_lead 4 infu.l.id 1
1 SIMPLE d eq_ref PRIMARY PRIMARY 4 infu.a.id_dealership 1
Upvotes: 0
Views: 68
Reputation: 562260
Use InnoDB instead of MyISAM. InnoDB caches both data and indexes, whereas MyISAM only caches indexes. InnoDB has other benefits too.
Do you really need all 15381 leads to be returned by this query? Or are you trying to query only a subset of leads, such as those that have assignments and dealerships?
Make sure you understand the type of join you're using. I suspect you're using left join when you need an inner join. It's probably causing the result set to be larger than it needs to be. Adding overhead for sorting, memory use, etc.
Choose data types more appropriately. Does every string have to be varchar(255)? Are you aware that varchars pad out to their maximum length in memory? You're even using varchar(255) to store an IP address. Also, you're using a signed integer to store dates?
Change the following index:
ALTER TABLE assignments ADD KEY assgn_lead_dealership (id_lead, id_dealership),
DROP KEY id_lead;
Upvotes: 2