I need help re-writing a query - This particular query sometimes runs super slow, other times runs in milli-seconds. But it’s been a major pain, I’m not against restructuring tables and no single table has more then 5000 rows in it.
The tables involved.
[code]CREATE TABLE domain_availability_monitoring (
ID int(11) NOT NULL AUTO_INCREMENT,
C_ID int(11) NOT NULL,
monitoring_schedule_id bigint(20) NOT NULL,
site_id int(11) NOT NULL,
monitoring_type_id int(11) NOT NULL,
last_scanned datetime DEFAULT NULL,
expiration_date datetime DEFAULT NULL,
raw_text text,
ls_able_to_parse bit(1) DEFAULT b’1’,
whois_id int(11) DEFAULT NULL,
PRIMARY KEY (ID),
KEY fk_domain_availability_monitoring_customers1 (C_ID),
KEY fk_domain_availability_monitoring_monitoring_schedule1 (monitoring_schedule_id),
KEY fk_domain_availability_monitoring_customers_sites1 (site_id),
KEY fk_domain_availability_monitoring_lu_da_monitoring_type1 (monitoring_type_id),
CONSTRAINT fk_domain_availability_monitoring_customers1 FOREIGN KEY (C_ID) REFERENCES customers (C_ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_domain_availability_monitoring_customers_sites1 FOREIGN KEY (site_id) REFERENCES customers_sites (site_id) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_domain_availability_monitoring_lu_da_monitoring_type1 FOREIGN KEY (monitoring_type_id) REFERENCES lu_da_monitoring_type (monitoring_type_id) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_domain_availability_monitoring_monitoring_schedule1 FOREIGN KEY (monitoring_schedule_id) REFERENCES monitoring_schedule (id) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2413 DEFAULT CHARSET=latin1$$
CREATE TABLE lu_time_zones (
id int(11) NOT NULL AUTO_INCREMENT,
offset decimal(3,1) NOT NULL,
name varchar(45) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY name_UNIQUE (name)
) ENGINE=InnoDB AUTO_INCREMENT=427 DEFAULT CHARSET=utf8$$
CREATE TABLE customers (
C_ID int(11) NOT NULL AUTO_INCREMENT,
C_Email varchar(80) NOT NULL,
C_Date_Added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
C_UniqueKey varchar(38) NOT NULL,
C_element_id char(36) DEFAULT NULL,
C_Last_Login timestamp NULL DEFAULT NULL,
C_Registering_IP varchar(45) DEFAULT NULL,
C_TimeZoneID int(11) DEFAULT ‘324’,
C_AdjustForDST tinyint(4) NOT NULL DEFAULT ‘0’,
PRIMARY KEY (C_ID),
UNIQUE KEY C_Email_UNIQUE (C_Email),
KEY fk_customers_lu_time_zones1_idx (C_TimeZoneID),
CONSTRAINT fk_customers_lu_time_zones1 FOREIGN KEY (C_TimeZoneID) REFERENCES lu_time_zones (id) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=4763 DEFAULT CHARSET=latin1$$
CREATE TABLE customers_sites (
site_id int(11) NOT NULL AUTO_INCREMENT,
C_ID int(11) NOT NULL,
url_or_ip varchar(255) DEFAULT NULL,
have_admin_access bit(1) DEFAULT b’0’,
active bit(1) DEFAULT b’0’,
ts_site_seal_approved date DEFAULT NULL,
ssl_enabled bit(1) DEFAULT b’0’,
PRIMARY KEY (site_id,C_ID),
KEY fk_customers_sites_customers1 (C_ID),
KEY idx_cs_url_ip (url_or_ip,ts_site_seal_approved),
CONSTRAINT fk_customers_sites_customers1 FOREIGN KEY (C_ID) REFERENCES customers (C_ID) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3565 DEFAULT CHARSET=utf8$$
[/code]
The Query. It returns the results I’m looking for - It just needs to be tuned, rewritten, or something else.
[php]SELECT dam.id, dam.expiration_date,cs.url_or_ip, ms.notify_who_email,
dam.raw_text, ms.frequency_in_days, dam.monitoring_type_id
FROM domain_availability_monitoring dam,
customers_sites cs,
monitoring_schedule ms,
customers c,
lu_time_zones tz
where dam.monitoring_schedule_id = ms.id and cs.site_id = dam.site_id and cs.active =1 and c.c_id = cs.c_id and tz.id = c.c_timezoneid
and ((dam.monitoring_type_id in (2,3)
and TIMESTAMPDIFF(MINUTE, date_add(dam.last_scanned, INTERVAL ms.frequency_in_days DAY),UTC_TIMESTAMP()) >= 0 or dam.last_scanned is null)
or (dam.monitoring_type_id = 1 and TIMESTAMPDIFF(MINUTE, date_add(dam.last_scanned, INTERVAL ms.frequency_in_days DAY),UTC_TIMESTAMP()) > 0
and (TIMESTAMPDIFF(MINUTE, date_sub(dam.expiration_date, INTERVAL ms.frequency_in_days DAY), UTC_TIMESTAMP() )) >= 0
or dam.expiration_date is null))[/php]
