MySql How to minimum value one row - mysql

I will send an email to my client when approaching the measurement date with php. Something measurement valid one year, something valid 3 years, etc... therefore one company's data one row. But I would like write, that how many days valid his measurement.
I hope yours understand my problem. Thanks in advance! Adam
I have a table with structure:
CREATE TABLE IF NOT EXISTS `meresek` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`COMP_ID` varchar(4) NOT NULL COMMENT 'What a company ID',
`Comp_V` int(11) NOT NULL DEFAULT '1' COMMENT 'What size number of sites',
`MERES_1` date DEFAULT NULL,
`MERES_END_1` date DEFAULT NULL,
`EV_MERES_2` date DEFAULT NULL,
`MERES_END_2` date DEFAULT NULL,
`MERES_3` date DEFAULT NULL,
`MERES_END_3` date DEFAULT NULL,
`MERES_4` date DEFAULT NULL,
`MERES_END_4` date DEFAULT NULL,
.
.
.
.
`MERES_END_8` date DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `COMP_ID` (`COMP_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=70;
INSERT INTO `meresek` ( `ID`, `COMP_ID`, `COMP_V`,`MERES_1`
`MERES_END_1`,
`EV_MERES_2`,
`MERES_END_2`,
`MERES_3`,
`MERES_END_3`,
.
.
.
`MERES_END_8) VALUES
(1, 'X1', 1, NULL, NULL, '2011-03-07', '2011-03-31', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(2, 'X2', 1, NULL, NULL, '2010-12-19', '2019-12-31', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(3, 'Y11', 1, NULL, NULL, '2011-05-12', '2014-12-31', NULL, NULL, NULL, NULL, '2011-05-12', '2020-05-12', '2011-05-12', '2020-05-12', NULL, NULL, NULL, NULL),
(4, 'Y12', 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2012-02-02', '2013-02-02', '2012-02-02', '2013-02-02')

select least(
coalesce(col_1,col_2....col_8),
coalesce(col_2,col_1....col_8),
coalesce(col_3,col_1....col_8),
coalesce(col_4,col_1....col_8),
coalesce(col_5,col_1....col_8),
coalesce(col_6,col_1....col_8),
coalesce(col_7,col_1....col_8),
coalesce(col_8,col_1....col_1)) where id = '35'

Related

Updating a stored JSON in Mysql Database

I am trying to update a Stored JSON in my database and i am unable to run the following update query. I have copied below a select statement and an update statement
SELECT
`core_animal_event`. `animal_id` AS `MilkingEvent_animalID`,
JSON_UNQUOTE(JSON_EXTRACT(`core_animal_event`.`additional_attributes`, '$."62"')) AS `MilkingEvent_milkCompositeLitres`,
coalesce(JSON_UNQUOTE(JSON_EXTRACT(`core_animal_event`.`additional_attributes`, '$."68"')) +
JSON_UNQUOTE(JSON_EXTRACT(`core_animal_event`.`additional_attributes`, '$."61"')) +
JSON_UNQUOTE(JSON_EXTRACT(`core_animal_event`.`additional_attributes`, '$."59"')))
FROM `core_animal_event` WHERE (`core_animal_event`. `event_type` = 2) AND (`core_animal_event`. `country_id` = '10');
UPDATE `core_animal`
SET
JSON_UNQUOTE(JSON_EXTRACT(`core_animal_event`.`additional_attributes`, '$."62"')) =
coalesce(JSON_UNQUOTE(JSON_EXTRACT(`core_animal_event`.`additional_attributes`, '$."68"')) +
JSON_UNQUOTE(JSON_EXTRACT(`core_animal_event`.`additional_attributes`, '$."61"')) +
JSON_UNQUOTE(JSON_EXTRACT(`core_animal_event`.`additional_attributes`, '$."59"')))
WHERE (`core_animal_event`. `event_type` = 2) AND (`core_animal_event`. `country_id` = '10')
The following is the sample data from which we have a stored JSON column called additional_attribute and animal_id which is unique
# animal_id, additional_attributes
'2576', '{\"59\": null, \"61\": null, \"62\": null, \"63\": null, \"64\": null, \"65\": null, \"66\": null, \"67\": null, \"68\": null, \"69\": \"1\", \"70\": \"2\", \"71\": \"1\", \"72\": null, \"73\": \"2\", \"74\": \"1\", \"75\": null, \"76\": null, \"77\": [\"1\"], \"78\": \"32\", \"79\": \"70\", \"80\": \"4\", \"81\": null, \"82\": null, \"83\": null, \"84\": \"Mkiwa\", \"85\": \"19280\", \"86\": \"2405\", \"87\": \"TNZ000192802405\", \"88\": \"Brownwhite\", \"89\": \"1565789020239.jpg\", \"90\": \"1565789049469.jpg\", \"96\": null, \"97\": null, \"98\": null, \"99\": \"1\", \"100\": null, \"101\": null, \"102\": null, \"103\": null, \"104\": null, \"105\": null, \"106\": null, \"107\": null, \"108\": null, \"109\": null, \"110\": null, \"111\": null, \"112\": null, \"113\": null, \"114\": null, \"115\": null, \"116\": null, \"117\": null, \"118\": null, \"119\": null, \"120\": null, \"121\": null, \"122\": null, \"123\": null, \"124\": null, \"125\": null, \"126\": null, \"127\": null, \"128\": null, \"129\": null, \"130\": null, \"131\": null, \"132\": null, \"133\": null, \"134\": null, \"135\": null, \"136\": null, \"137\": null, \"138\": null, \"139\": null, \"141\": null, \"142\": null, \"143\": null, \"144\": null, \"145\": null}'
The following is an example of a create statement
CREATE TABLE `core_animal_event` (
`id` int NOT NULL AUTO_INCREMENT,
`animal_id` int NOT NULL,
`event_type` int NOT NULL,
`additional_attributes` json DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `org_id` (`country_id`),
KEY `animal_id` (`animal_id`),
KEY `event_type` (`event_type`),
CONSTRAINT `core_animal_event_ibfk_1` FOREIGN KEY (`animal_id`) REFERENCES `core_animal` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=941817 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=COMPACT;
Escape characters are not necessary here. I even think they may be the source of the problem.
Here's my code for the tests :
CREATE TABLE `core_animal_event` (
`animal_id` int(11) NOT NULL AUTO_INCREMENT,
`additional_attributes` json DEFAULT NULL,
`event_type` int(11) NOT NULL,
`country_id` int(11) NOT NULL,
PRIMARY KEY (`animal_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
INSERT INTO core_animal_event
(animal_id, additional_attributes, event_type, country_id)
VALUES(1, '{ "59": null, "61": null, "62": null, "63": null, "64": null, "65": null, "66": null, "67": null, "68": null, "69": "1", "70": "2", "71": "1", "72": null, "73": "2", "74": "1", "75": null, "76": null, "77": ["1"], "78": "32", "79": "70", "80": "4", "81": null, "82": null, "83": null, "84": "Mkiwa", "85": "19280", "86": "2405", "87": "TNZ000192802405", "88": "Brownwhite", "89": "1565789020239.jpg", "90": "1565789049469.jpg", "96": null, "97": null, "98": null, "99": "1", "100": null, "101": null, "102": null, "103": null, "104": null, "105": null, "106": null, "107": null, "108": null, "109": null, "110": null, "111": null, "112": null, "113": null, "114": null, "115": null, "116": null, "117": null, "118": null, "119": null, "120": null, "121": null, "122": null, "123": null, "124": null, "125": null, "126": null, "127": null, "128": null, "129": null, "130": null, "131": null, "132": null, "133": null, "134": null, "135": null, "136": null, "137": null, "138": null, "139": null, "141": null, "142": null, "143": null, "144": null, "145": null}', 2, 10);
Without escape characters \, your SELECT query work.
For your update, here's an example :
UPDATE core_animal_event
SET additional_attributes = json_set(additional_attributes, '$."62"',
COALESCE(JSON_UNQUOTE(JSON_EXTRACT(additional_attributes, '$."68"')) +
JSON_UNQUOTE(JSON_EXTRACT(additional_attributes, '$."61"')) +
JSON_UNQUOTE(JSON_EXTRACT(additional_attributes, '$."59"')))
)
WHERE (event_type = 2) AND (country_id = '10');
//-------
EDIT:
Be careful when using JSON_EXTRACT and COALESCE function, in case all values are null, the returned value is 0, not a null value.
EDIT 2:
Akina is right, your COALESCE function is no good... As you do, it's an addition (but maybe that's what you want...)
EDIT 3:
If you want to use COALESCE here's an example:
SELECT animal_id AS colID, JSON_UNQUOTE(JSON_EXTRACT(additional_attributes, '$."62"')) AS col62,
COALESCE (
IF(JSON_TYPE(JSON_EXTRACT(additional_attributes, '$."68"'))='NULL', null, JSON_EXTRACT(additional_attributes, '$."68"')),
IF(JSON_TYPE(JSON_EXTRACT(additional_attributes, '$."59"'))='NULL', null, JSON_EXTRACT(additional_attributes, '$."59"')),
IF(JSON_TYPE(JSON_EXTRACT(additional_attributes, '$."61"'))='NULL', null, JSON_EXTRACT(additional_attributes, '$."61"'))
) AS result_of_coalesce
FROM core_animal_event WHERE (event_type = 2) AND (country_id = '10');

mysql sql qyery performace slow for 300k result retrieval

I have 300k data in my database.through ajax and php i am retrieving result from table. when i try to retrieve this data using below query then it is very slow.
is there any other way to write this query so that i get result very fast?
SELECT e_user_id,
assembly_no,
polling_station_number,
polling_station_name_hindi,
polling_station_name_eng,
serial_number,
section_no,
section_name_hindi,
section_name_eng,
house_no,
e_first_name_hindi,
e_first_name_eng,
e_last_name_eng,
e_relation_type,
e_relative_first_name_hindi,
e_relative_last_name_hindi,
e_relative_first_name_eng,
e_relative_last_name_eng,
id_card_number,
gender,
age,
dob,
contact_number,
uid
FROM vw_absent_record
WHERE polling_station_number IN ( 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
17, 18, 19, 20,
21, 22, 23, 24,
25, 26, 27 )
AND assembly_no = 48
ORDER BY e_user_id ASC
this is the index query and e_voter_list table
CREATE view vw_absent_record
AS
SELECT e_voter_list.e_user_id,
e_voter_list.assembly_no,
e_voter_list.polling_station_number,
e_voter_list.polling_station_name_hindi,
e_voter_list.polling_station_name_eng,
e_voter_list.serial_number,
e_voter_list.section_no,
e_voter_list.section_name_hindi,
e_voter_list.section_name_eng,
e_voter_list.house_no,
e_voter_list.e_first_name_hindi,
e_voter_list.e_first_name_eng,
e_voter_list.e_last_name_eng,
e_voter_list.e_relation_type,
e_voter_list.e_relative_first_name_hindi,
e_voter_list.e_relative_last_name_hindi,
e_voter_list.e_relative_first_name_eng,
e_voter_list.e_relative_last_name_eng,
e_voter_list.id_card_number,
e_voter_list.gender,
e_voter_list.age,
e_voter_list.dob,
e_voter_list.contact_number,
e_voter_list.uid
FROM e_voter_list
LEFT OUTER JOIN vw_attendance_record
ON
( e_voter_list.e_user_id = vw_attendance_record.e_user_id )
WHERE vw_attendance_record.e_user_id IS NULL
this is my e_voter_list table
CREATE TABLE `e_voter_list` (
`e_user_id` bigint(11) NOT NULL,
`assembly_no` bigint(8) NOT NULL DEFAULT '0',
`polling_station_number` int(4) NOT NULL DEFAULT '0',
`polling_station_name_hindi` varchar(350) DEFAULT NULL,
`polling_station_name_eng` varchar(350) DEFAULT NULL,
`serial_number` int(4) NOT NULL DEFAULT '0',
`section_no` int(5) DEFAULT NULL,
`section_name_hindi` varchar(450) DEFAULT NULL,
`section_name_eng` varchar(450) DEFAULT NULL,
`house_no` varchar(150) DEFAULT NULL,
`e_first_name_hindi` varchar(150) DEFAULT NULL,
`e_last_name_hindi` varchar(150) DEFAULT NULL,
`e_first_name_eng` varchar(150) DEFAULT NULL,
`e_last_name_eng` varchar(150) DEFAULT NULL,
`e_relation_type` varchar(5) DEFAULT NULL,
`e_relative_first_name_hindi` varchar(150) DEFAULT NULL,
`e_relative_last_name_hindi` varchar(150) DEFAULT NULL,
`e_relative_first_name_eng` varchar(150) DEFAULT NULL,
`e_relative_last_name_eng` varchar(150) DEFAULT NULL,
`id_card_number` varchar(30) NOT NULL,
`gender` varchar(8) DEFAULT NULL,
`age` varchar(10) DEFAULT NULL,
`dob` varchar(30) DEFAULT NULL,
`contact_number` varchar(15) DEFAULT NULL,
`uid` varchar(25) DEFAULT NULL,
`status_type` varchar(20) DEFAULT NULL,
`year_of_birth` int(4) DEFAULT NULL,
`date_of_attendance` date DEFAULT NULL,
`attendance_status` int(2) NOT NULL DEFAULT '0',
`e_attendance_timestamp` datetime DEFAULT NULL,
`update_agent_id` int(7) DEFAULT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Have you used indexing in your table? If not then please implement indexing on columns then your select query will be fast. Also I will suggest you to get records by using limit and offset like this:
SELECT e_user_id,assembly_no,polling_station_number,polling_station_name_hindi,polling_station_name_eng,serial_number,section_no,section_name_hindi,section_name_eng,house_no,e_first_name_hindi,e_first_name_eng,e_last_name_eng,e_relation_type,e_relative_first_name_hindi,e_relative_last_name_hindi,e_relative_first_name_eng,e_relative_last_name_eng,id_card_number,gender,age,dob,contact_number,uid FROM vw_absent_record where polling_station_number IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27) and assembly_no=48 ORDER BY e_user_id ASC LIMIT 10,0

i want to optimize mysql query

Please guys help me to optimize the below query
SELECT
`dti`.`CompanyId`,
`dti`.`Samiti`,
`dti`.`toll_date`,
`dti`.`MajorFee`,
`dti`.`MinorFee`,
`dti`.`SawalFee`,
SUM(dti.Tmwt) as Tmwt,
SUM(dti.Localminor) as Localminor,
SUM(dti.Swt) as Swt,
SUM(dti.Twt) as Twt,
SUM(((dti.Tmwt * dti.MajorFee) + (dti.Localminor * dti.MinorFee) + (dti.Swt * dti.SawalFee))) as total_wages,
SUM((dti.Twt * dti.govt_charges)) as govt_deduction,
SUM((((dti.Tmwt * dti.MajorFee) + (dti.Localminor * dti.MinorFee) + (dti.Swt * dti.SawalFee)) - (dti.Twt * dti.govt_charges))) as net_amount,
(SELECT (SUM(ld.amount) + SUM(ld.advance_deduction))
FROM psac_liability_deduction ld
WHERE ld.status = "Active" AND
ld.from_date >="2017-08-24" AND
ld.to_date <="2017-08-31" AND
ld.deducted_for = dti.CompanyId
) as group_liability_deduction,
(SELECT CONCAT(SUM(wi.GroupLiabilityDeduction), "|", SUM(wi.AdvanceWagesDeduction))
FROM psac_wagesitem wi
WHERE wi.status="Active" AND
wi.from_date >= "2017-08-24" AND
wi.to_date <= "2017-08-31" AND
wi.MainGroup=dti.Samiti AND
wi.FishermanId=dti.CompanyId
) as wages_deduction,
(SELECT CONCAT(SUM(cdp.product_liability), "|", SUM(cdp.wages_liability))
FROM psac_cash_deposited_payment cdp
WHERE cdp.status="Active" AND
cdp.deposit_date >= "2017-08-24" AND
cdp.deposit_date <= "2017-08-31" AND
cdp.maingroup_id=dti.Samiti AND
cdp.fisherman_id=dti.CompanyId
) as cash_deposited,
`fm`.`Name` as `fishername`,
`fm`.`Code` as `fishername_code`,
`fm`.`Bank`,
`fm`.`IfscCode`,
`fm`.`AccountNo`
FROM `psac_dailytollinfo` `dti`
LEFT JOIN `psac_fisherman` `fm` ON `fm`.`ID`=`dti`.`CompanyId`
WHERE
`dti`.`status` = 'Active' AND
`dti`.`toll_date` >= '2017-08-24' AND
`dti`.`toll_date` <= '2017-08-31'
GROUP BY `dti`.`toll_date`, `dti`.`CompanyId`
ORDER BY `dti`.`toll_date` ASC
please help me to optimize this query. If i remove sub queries it will works perfect but with subqueries it takes too much time.
below are table structures
psac_dailytollinfo table
CREATE TABLE `psac_dailytollinfo` (
`ID` int(11) NOT NULL,
`toll_date` date NOT NULL,
`Point` int(11) NOT NULL,
`group_type` int(11) NOT NULL,
`Samiti` int(11) NOT NULL,
`DailytollId` int(11) NOT NULL,
`CompanyId` int(11) NOT NULL,
`Name` varchar(250) NOT NULL,
`govt_charges` float(15,2) NOT NULL,
`MajorFee` float(15,2) NOT NULL,
`MinorFee` float(15,2) NOT NULL,
`SawalFee` float(15,2) NOT NULL,
`Cqty` varchar(150) NOT NULL,
`Cwt` varchar(150) NOT NULL,
`Rqty` varchar(150) NOT NULL,
`Rwt` varchar(150) NOT NULL,
`Mqty` varchar(150) NOT NULL,
`Mwt` varchar(150) NOT NULL,
`Kqty` varchar(150) NOT NULL,
`Kwt` varchar(150) NOT NULL,
`Aqty` varchar(150) NOT NULL,
`Awt` varchar(150) NOT NULL,
`Sqty` varchar(11) NOT NULL,
`Swt` varchar(11) NOT NULL,
`Lqty` varchar(150) NOT NULL,
`Lwt` varchar(150) NOT NULL,
`Localminor` varchar(150) NOT NULL,
`Tmqty` varchar(150) NOT NULL,
`Tmwt` varchar(150) NOT NULL,
`Tqty` varchar(150) NOT NULL,
`Twt` varchar(150) NOT NULL,
`added_by` int(11) NOT NULL,
`updated_by` int(11) NOT NULL,
`added_date` datetime NOT NULL,
`updated_date` datetime NOT NULL,
`action_microtime` varchar(20) NOT NULL,
`status` enum('Active','Inactive','Deleted') NOT NULL DEFAULT 'Active'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
psac_liability_deduction table
CREATE TABLE `psac_liability_deduction` (
`ID` bigint(20) NOT NULL,
`wages_id` int(11) NOT NULL,
`wages_item_id` int(11) NOT NULL,
`amount` float(15,2) NOT NULL,
`advance_deduction` float(11,2) NOT NULL,
`group_type_id` int(11) NOT NULL,
`maingroup_id` int(11) NOT NULL,
`deducted_by` int(11) NOT NULL,
`deducted_for` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
`status` enum('Active','Inactive','Deleted') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Active',
`added_by` int(11) NOT NULL,
`updated_by` int(11) NOT NULL,
`added_date` datetime NOT NULL,
`updated_date` datetime NOT NULL,
`action_microtime` varchar(50) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
psac_wagesitem table
CREATE TABLE `psac_wagesitem` (
`ID` int(11) NOT NULL,
`wages_for` enum('Fisherman','Group') NOT NULL DEFAULT 'Fisherman',
`wages_id` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
`group_type_id` int(11) NOT NULL,
`MainGroup` int(11) NOT NULL,
`FishermanId` int(11) NOT NULL,
`MajorFee` float(15,2) NOT NULL,
`MinorFee` float(15,2) NOT NULL,
`SawalFee` float(15,2) NOT NULL,
`major_wt` float(15,2) NOT NULL,
`minor_wt` float(15,2) NOT NULL,
`sawal_wt` float(15,2) NOT NULL,
`major_wage` float(15,2) NOT NULL,
`minor_wage` float(15,2) NOT NULL,
`sawal_wage` float(15,2) NOT NULL,
`TotalWage` float(15,2) NOT NULL,
`group_liability` float(15,2) NOT NULL,
`advance_wages` float(15,2) NOT NULL,
`GovDeduction` float(15,2) NOT NULL,
`GroupLiabilityDeduction` float(15,2) NOT NULL,
`AdvanceWagesDeduction` float(15,2) NOT NULL,
`final_wages` float(15,2) NOT NULL,
`added_by` int(11) NOT NULL,
`updated_by` int(11) NOT NULL,
`added_date` datetime NOT NULL,
`updated_date` datetime NOT NULL,
`action_microtime` varchar(20) NOT NULL,
`status` enum('Active','Inactive','Deleted') NOT NULL DEFAULT 'Active',
`editable` enum('Lock','Unlock') NOT NULL DEFAULT 'Unlock'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
psac_cash_deposited_payment table
CREATE TABLE `psac_cash_deposited_payment` (
`deposit_id` int(11) NOT NULL,
`deposited_by` enum('Fisherman','Group') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Fisherman',
`deposit_date` date NOT NULL,
`group_type_id` int(11) NOT NULL,
`maingroup_id` int(11) NOT NULL,
`fisherman_id` int(11) NOT NULL,
`product_liability` float(11,2) NOT NULL,
`wages_liability` float(11,2) NOT NULL,
`receipt_number` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`remark` text COLLATE utf8_unicode_ci NOT NULL,
`status` enum('Active','Inactive','Deleted') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Active',
`editable` enum('Lock','Unlock') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Unlock',
`added_by` int(11) NOT NULL,
`added_date` datetime NOT NULL,
`updated_by` int(11) NOT NULL,
`updated_date` datetime NOT NULL,
`action_microtime` varchar(20) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
You understand that you are looking at a full 8 days? If you only wanted one week, change <= to <.
Switch all tables to InnoDB.
Have a PRIMARY KEY for every table -- either a 'natural' PK (composed of one or more columns that, together, uniquely define each row), or an AUTO_INCREMENT.
dti needs INDEX(status, toll_date)
Don't use (m,n) on FLOAT, it leads to an extra roundoff.
Don't use FLOAT for money, it leads to a roundoff.
FLOAT (with or without (m,n)) contains no more than 7 significant digits.
Consider DECIMAL(11,2) instead of float(11,2).
Be cautious about using latin1 in one table and utf8 in another -- if you need to JOIN on a VARCHAR; it must have the same charset and collation in order to use an index.
Where practical, make the ORDER BY identical to the GROUP BY.
These composite indexes that are likely to help performance:
dti: INDEX(status, toll_date)
ld: INDEX(status, deducted_for, from_date)
ld: INDEX(status, deducted_for, to_date)
wi: INDEX(status, MainGroup, FishermanId, from_date)
wi: INDEX(status, MainGroup, FishermanId, to_date)
cdp: INDEX(status, maingroup_id, fisherman_id, deposit_date)
(The date must be last; the other column(s) can be in any order.)
If you still have performance problems with the subqueries after you have added those indexes, let's see EXPLAIN SELECT ... so we can look again.
Don't splay an array across columns:
`Cqty` varchar(150) NOT NULL,
`Cwt` varchar(150) NOT NULL,
etc
Consider having qty and wt as two columns in another table.
Could there be two different values for MajorFee in a single day for a single company? That, and other things say that the GROUP BY is improperly formed.

Join on large table getting slower

I have 2 innoDB tables, One has 15166897 records and an other has 700000 records, sometime when there is load on the server a simple join query based on email address on these tables took much time to execute and some time is executes quickly. However, both the tables are properly indexed.
We have around 800 tables into our database, DB size is 40GB. We are running t2.medium RDS instance in AWS having 4GB of RAM in which 3GB is allocated for INNO DB BUFFER POOL.
I have the following questions, Please suggest on it.
Is it a good idea to break the large table into small tables so that records on the one table become less, Does this will improve the query performance?
Does the large table over the time downgrade the performance of the query?
Is this RDS configuration is enough for DB, OR should need to improve the configuration?
EDIT:
Below is my table schema and index definition:
Table 1: Total Records: 7,00,000
CREATE TABLE IF NOT EXISTS `tbl_contact_master` (
`id` int(11) NOT NULL,
`first_name` varchar(60) NOT NULL,
`last_name` varchar(60) NOT NULL,
`email_address` varchar(250) NOT NULL,
`agency_name` varchar(150) NOT NULL,
`state` varchar(30) NOT NULL,
`zip_code` varchar(20) NOT NULL,
`srch_zip` varchar(20) NOT NULL,
`title` varchar(100) NOT NULL,
`street` varchar(255) NOT NULL,
`street2` varchar(255) NOT NULL,
`city` varchar(30) NOT NULL,
`country` varchar(30) NOT NULL,
`phone` varchar(20) NOT NULL,
`ext` varchar(20) NOT NULL,
`fax` varchar(20) NOT NULL,
`years_of_experience` varchar(50) DEFAULT NULL,
`owner_manager` varchar(100) NOT NULL,
`agency_type` varchar(100) NOT NULL,
`agency_sales` varchar(100) NOT NULL,
`agency_website` varchar(100) NOT NULL,
`agency_host` varchar(100) NOT NULL,
`agency_host_name` varchar(100) NOT NULL,
`agency_affiliation` varchar(100) NOT NULL,
`agent_sales` varchar(100) NOT NULL,
`id_number_type` varchar(16) NOT NULL,
`id_number` varchar(15) NOT NULL,
`destination1` varchar(100) NOT NULL,
`destination2` varchar(100) NOT NULL,
`destination3` varchar(100) NOT NULL,
`travel_type1` varchar(100) NOT NULL,
`travel_type2` varchar(100) NOT NULL,
`travel_type3` varchar(100) NOT NULL,
`travel_type4` varchar(100) NOT NULL,
`update_first_name` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_last_name` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_email_address` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_name` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_state` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_zip_code` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_title` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_street` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_street2` datetime NOT NULL,
`update_city` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_country` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_phone` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_ext` datetime NOT NULL,
`update_fax` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_years_of_experience` datetime NOT NULL,
`update_owner_manager` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_type` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_sales` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_website` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_host` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_host_name` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agency_affiliation` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_agent_sales` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_id_number_type` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_id_number` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_destination1` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_destination2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_destination3` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_travel_type1` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_travel_type2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_travel_type3` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_travel_type4` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`create_status` tinyint(1) NOT NULL COMMENT '1 for website or 0 for add by csv',
`created_date` datetime NOT NULL,
`modified_date` datetime NOT NULL,
`submited_date` datetime NOT NULL,
`FirstPageDate` datetime NOT NULL,
`is_retired` tinyint(1) NOT NULL COMMENT '0 = No, 1 = Yes',
`update_is_retired` datetime NOT NULL,
`ip_address` varchar(30) NOT NULL,
`is_bademail` tinyint(1) NOT NULL,
`update_is_bademail` datetime NOT NULL,
`is_mice` tinyint(4) NOT NULL DEFAULT '0',
`update_is_mice` datetime NOT NULL,
`mice_company_disc` mediumint(5) unsigned NOT NULL,
`update_mice_company_disc` datetime NOT NULL,
`mice_meeting_region_plan` text NOT NULL,
`update_mice_meeting_region_plan` datetime NOT NULL,
`mice_ida` text NOT NULL,
`update_mice_ida` datetime NOT NULL,
`mice_associations` text NOT NULL,
`update_mice_associations` datetime NOT NULL,
`mice_meeting_regions` text NOT NULL,
`update_mice_meeting_regions` datetime NOT NULL,
`mice_average_attendance` mediumint(5) unsigned NOT NULL,
`update_mice_average_attendance` datetime NOT NULL,
`mice_annual_budget` mediumint(5) unsigned NOT NULL,
`update_mice_annual_budget` datetime NOT NULL,
`mice_facilities` text NOT NULL,
`update_mice_facilities` datetime NOT NULL,
`mice_annual_peak_rooms` mediumint(5) unsigned NOT NULL,
`update_mice_annual_peak_rooms` datetime NOT NULL,
`mice_nof_meetings` mediumint(5) unsigned NOT NULL,
`update_mice_nof_meetings` datetime NOT NULL,
`mice_job_resp_level` mediumint(5) unsigned NOT NULL,
`update_mice_job_resp_level` datetime NOT NULL,
`mice_event_resp_level` mediumint(5) unsigned NOT NULL,
`update_mice_event_resp_level` datetime NOT NULL,
`mice_is_planning` mediumint(5) unsigned NOT NULL,
`update_mice_is_planning` datetime NOT NULL,
`mice_experience` mediumint(5) unsigned NOT NULL,
`update_mice_experience` datetime NOT NULL,
`mice_primary_job` mediumint(5) unsigned NOT NULL,
`update_mice_primary_job` datetime NOT NULL,
`mice_company_size` mediumint(5) unsigned NOT NULL,
`update_mice_company_size` datetime NOT NULL,
`mice_primary_business` mediumint(5) unsigned NOT NULL,
`update_mice_primary_business` datetime NOT NULL,
`mice_primary_business_other` text NOT NULL,
`mice_event_specialty` text NOT NULL,
`update_mice_event_specialty` datetime NOT NULL,
`mice_created_date` datetime NOT NULL,
`mice_modified_date` datetime NOT NULL,
`mice_submitted_date` datetime NOT NULL,
`is_ta` tinyint(4) NOT NULL DEFAULT '0',
`update_is_ta` datetime NOT NULL,
`ta_created_date` datetime NOT NULL,
`ta_modified_date` datetime NOT NULL,
`ta_submitted_date` datetime NOT NULL,
`is_mice_retired` tinyint(4) NOT NULL DEFAULT '0',
`update_is_mice_retired` datetime NOT NULL,
`not_ta_by` tinyint(2) NOT NULL COMMENT '0 for default, 1 for superadmin, 2 for contact',
`update_not_ta_by` datetime NOT NULL,
`not_mice_by` tinyint(2) NOT NULL COMMENT '0 for default, 1 for superadmin, 2 for contact',
`update_not_mice_by` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1096438 DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tbl_contact_master`
--
ALTER TABLE `tbl_contact_master`
ADD PRIMARY KEY (`id`),
ADD KEY `email_address` (`email_address`),
ADD KEY `is_ta` (`is_ta`),
ADD KEY `mice_created_date` (`mice_created_date`),
ADD KEY `mice_modified_date` (`mice_modified_date`),
ADD KEY `mice_submitted_date` (`mice_submitted_date`),
ADD KEY `ta_created_date` (`ta_created_date`),
ADD KEY `ta_modified_date` (`ta_modified_date`),
ADD KEY `ta_submitted_date` (`ta_submitted_date`),
ADD KEY `is_mice_retired` (`is_mice_retired`),
ADD KEY `is_mice` (`is_mice`),
ADD KEY `is_bademail` (`is_bademail`),
ADD KEY `is_retired` (`is_retired`),
ADD KEY `created_date` (`created_date`),
ADD KEY `modified_date` (`modified_date`),
ADD KEY `submited_date` (`submited_date`),
ADD KEY `srch_zip` (`srch_zip`);
Table 2:
CREATE TABLE IF NOT EXISTS `tbl_master_partition` (
`contact_id` int(10) unsigned NOT NULL,
`inactive_group` varchar(200) DEFAULT NULL COMMENT '1=15,2=30,3=45,4=60',
`inactive_from` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tbl_master_partition`
--
ALTER TABLE `tbl_master_partition`
ADD KEY `contact_id` (`contact_id`), ADD KEY `inactive_group` (`inactive_group`);
Table 3: Total Records: 14400000
CREATE TABLE IF NOT EXISTS `tbl_mandrill_email_mapping` (
`id` bigint(20) NOT NULL,
`log_id` int(11) NOT NULL,
`batch_id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`m_id` varchar(50) COLLATE utf8_unicode_ci NOT NULL COMMENT 'unique mandrill message id',
`s_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`open` int(11) NOT NULL DEFAULT '0',
`click` int(11) NOT NULL DEFAULT '0',
`send` int(11) NOT NULL DEFAULT '0',
`hard_bounce` int(11) NOT NULL DEFAULT '0',
`soft_bounce` int(11) NOT NULL DEFAULT '0',
`reject` int(11) NOT NULL DEFAULT '0',
`spam` int(11) NOT NULL DEFAULT '0',
`unsub` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=15131814 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tbl_mandrill_email_mapping`
--
ALTER TABLE `tbl_mandrill_email_mapping`
ADD PRIMARY KEY (`id`),
ADD KEY `m_id` (`m_id`),
ADD KEY `email` (`email`),
ADD KEY `hard_bounce` (`hard_bounce`),
ADD KEY `reject` (`reject`),
ADD KEY `open` (`open`),
ADD KEY `click` (`click`),
ADD KEY `send` (`send`),
ADD KEY `log_id` (`log_id`),
ADD KEY `batch_id` (`batch_id`),
ADD KEY `s_id` (`s_id`);
Missing TABLE added
CREATE TABLE IF NOT EXISTS `tbl_contact_173_230` (
`id` int(11) NOT NULL,
`contact_id` int(11) NOT NULL,
`salutation` varchar(10) NOT NULL,
`first_name` varchar(60) NOT NULL,
`last_name` varchar(60) NOT NULL,
`email_address` varchar(250) NOT NULL,
`secondary_email_address` varchar(250) NOT NULL,
`agency_name` varchar(150) NOT NULL,
`state` varchar(30) NOT NULL,
`zip_code` varchar(20) NOT NULL,
`srch_zip` varchar(20) NOT NULL,
`title` varchar(255) NOT NULL,
`street` varchar(255) NOT NULL,
`street2` varchar(255) NOT NULL,
`city` varchar(30) NOT NULL,
`country` varchar(30) NOT NULL,
`phone` varchar(20) NOT NULL,
`ext` varchar(20) NOT NULL,
`fax` varchar(20) NOT NULL,
`owner_manager` varchar(140) DEFAULT NULL,
`years_of_experience` varchar(40) DEFAULT NULL,
`agency_type` varchar(140) NOT NULL,
`agency_sales` varchar(140) NOT NULL,
`agency_website` varchar(140) NOT NULL,
`agency_host` varchar(140) DEFAULT NULL,
`agency_host_name` varchar(140) NOT NULL,
`agency_affiliation` varchar(140) NOT NULL,
`agent_sales` varchar(140) NOT NULL,
`id_number_type` varchar(16) NOT NULL,
`id_number` varchar(15) NOT NULL,
`destination1` varchar(140) NOT NULL,
`destination2` varchar(140) NOT NULL,
`destination3` varchar(140) NOT NULL,
`travel_type1` varchar(140) NOT NULL,
`travel_type2` varchar(140) NOT NULL,
`travel_type3` varchar(140) NOT NULL,
`travel_type4` varchar(140) NOT NULL,
`update_first_name` datetime NOT NULL,
`update_last_name` datetime NOT NULL,
`update_email_address` datetime NOT NULL,
`update_agency_name` datetime NOT NULL,
`update_state` datetime NOT NULL,
`update_zip_code` datetime NOT NULL,
`update_title` datetime NOT NULL,
`update_street` datetime NOT NULL,
`update_street2` datetime NOT NULL,
`update_city` datetime NOT NULL,
`update_country` datetime NOT NULL,
`update_phone` datetime NOT NULL,
`update_ext` datetime NOT NULL,
`update_fax` datetime NOT NULL,
`update_owner_manager` datetime NOT NULL,
`update_years_of_experience` datetime NOT NULL,
`update_agency_type` datetime NOT NULL,
`update_agency_sales` datetime NOT NULL,
`update_agency_website` datetime NOT NULL,
`update_agency_host` datetime NOT NULL,
`update_agency_host_name` datetime NOT NULL,
`update_agency_affiliation` datetime NOT NULL,
`update_agent_sales` datetime NOT NULL,
`update_id_number_type` datetime NOT NULL,
`update_id_number` datetime NOT NULL,
`update_destination1` datetime NOT NULL,
`update_destination2` datetime NOT NULL,
`update_destination3` datetime NOT NULL,
`update_travel_type1` datetime NOT NULL,
`update_travel_type2` datetime NOT NULL,
`update_travel_type3` datetime NOT NULL,
`update_travel_type4` datetime NOT NULL,
`opt_in_marketing` varchar(10) DEFAULT NULL,
`travel_pro_user` varchar(10) DEFAULT NULL,
`create_status` tinyint(1) NOT NULL,
`crm_created_date` datetime NOT NULL,
`crm_modified_date` datetime NOT NULL,
`crm_submited_date` datetime NOT NULL,
`crm_sync_date` datetime NOT NULL,
`created_date` datetime NOT NULL,
`modified_date` datetime NOT NULL,
`submited_date` datetime NOT NULL,
`offer_entry` datetime NOT NULL,
`sync_date` datetime NOT NULL,
`crm_status` tinyint(1) NOT NULL,
`created_by` int(11) NOT NULL,
`is_retired` tinyint(1) NOT NULL,
`update_is_retired` datetime NOT NULL,
`is_bademail` tinyint(1) NOT NULL,
`update_is_bademail` datetime NOT NULL,
`is_ta` tinyint(4) NOT NULL DEFAULT '0',
`update_is_ta` datetime NOT NULL,
`ta_created_date` datetime NOT NULL,
`ta_modified_date` datetime NOT NULL,
`ta_submitted_date` datetime NOT NULL,
`mice_company_disc` text NOT NULL,
`update_mice_company_disc` datetime NOT NULL,
`mice_meeting_region_plan` text NOT NULL,
`update_mice_meeting_region_plan` datetime NOT NULL,
`mice_ida` text NOT NULL,
`update_mice_ida` datetime NOT NULL,
`mice_associations` text NOT NULL,
`update_mice_associations` datetime NOT NULL,
`mice_meeting_regions` text NOT NULL,
`update_mice_meeting_regions` datetime NOT NULL,
`mice_average_attendance` mediumint(5) unsigned NOT NULL,
`update_mice_average_attendance` datetime NOT NULL,
`mice_annual_budget` mediumint(5) unsigned NOT NULL,
`update_mice_annual_budget` datetime NOT NULL,
`mice_facilities` text NOT NULL,
`update_mice_facilities` datetime NOT NULL,
`mice_annual_peak_rooms` mediumint(5) unsigned NOT NULL,
`update_mice_annual_peak_rooms` datetime NOT NULL,
`mice_nof_meetings` mediumint(5) unsigned NOT NULL,
`update_mice_nof_meetings` datetime NOT NULL,
`mice_job_resp_level` mediumint(5) unsigned NOT NULL,
`update_mice_job_resp_level` datetime NOT NULL,
`mice_event_resp_level` mediumint(5) unsigned NOT NULL,
`update_mice_event_resp_level` datetime NOT NULL,
`mice_is_planning` mediumint(5) unsigned NOT NULL,
`update_mice_is_planning` datetime NOT NULL,
`mice_experience` mediumint(5) unsigned NOT NULL,
`update_mice_experience` datetime NOT NULL,
`mice_primary_job` mediumint(5) unsigned NOT NULL,
`update_mice_primary_job` datetime NOT NULL,
`mice_company_size` mediumint(5) unsigned NOT NULL,
`update_mice_company_size` datetime NOT NULL,
`mice_primary_business` mediumint(5) unsigned NOT NULL,
`mice_primary_business_other` text NOT NULL,
`update_mice_primary_business` datetime NOT NULL,
`mice_event_specialty` text NOT NULL,
`update_mice_event_specialty` datetime NOT NULL,
`is_mice` tinyint(4) NOT NULL DEFAULT '0',
`update_is_mice` datetime NOT NULL,
`mice_created_date` datetime NOT NULL,
`mice_modified_date` datetime NOT NULL,
`mice_submitted_date` datetime NOT NULL,
`is_mice_retired` tinyint(4) NOT NULL DEFAULT '0',
`update_is_mice_retired` datetime NOT NULL,
`recurring_opt_out` tinyint(1) unsigned NOT NULL DEFAULT '0',
`custom_648` varchar(255) NOT NULL,
`custom_649` varchar(255) NOT NULL,
`custom_650` varchar(255) NOT NULL,
`custom_717` varchar(255) NOT NULL,
`custom_718` varchar(255) NOT NULL,
`custom_719` varchar(255) NOT NULL,
`custom_1369` varchar(255) NOT NULL,
`custom_1454` varchar(255) NOT NULL,
`custom_1455` varchar(255) NOT NULL,
`custom_1469` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=103603 DEFAULT CHARSET=latin1;
--
-- Indexes for table `tbl_contact_173_230`
--
ALTER TABLE `tbl_contact_173_230`
ADD PRIMARY KEY (`id`),
ADD KEY `email_address` (`email_address`),
ADD KEY `contact_id` (`contact_id`),
ADD KEY `zip_code` (`zip_code`),
ADD KEY `srch_zip` (`srch_zip`),
ADD KEY `ta_submitted_date` (`ta_submitted_date`);
Below are the queries which getting slower or sometime just get stuck for an hrs.
1.
SELECT cm.id AS contact_id, mem.email, mem.open
FROM tbl_mandrill_email_mapping AS mem
JOIN tbl_contact_master AS cm ON cm.email_address = mem.email
WHERE mem.log_id = XXXXX
2.
SELECT COUNT(crm.id) as total_record, SUM(UPPER(opt_in_marketing)='NO') as unsub_email_count,
SUM(is_bademail=1) as invalid_email_count, COUNT(DISTINCT mp.`contact_id`) as inactive_email_count,
SUM(is_retired=1
OR is_mice_retired=1
) as retired_count
FROM (`tbl_contact_173_230` AS crm)
INNER JOIN `tbl_mandrill_email_mapping` as mem
ON `mem`.`email` = `crm`.`email_address`
LEFT JOIN`tbl_master_partition` AS mp
ON `mp`.`contact_id` = `crm`.`contact_id`
AND crm.`ta_submitted_date` < '2016-10-19'
AND FIND_IN_SET(5, mp.`inactive_group`) > 0
WHERE `crm`.`email_address` != '' AND`mem`.`log_id` = 'xxxx'
AND `mem`.`open` = 0
AND `mem`.`hard_bounce` = 0
AND `mem`.`reject` = 0
AND `mem`.`spam` = 0
ORDER BY `crm`.`id` ASC
Edit Note:
I have notice that, it mostly creates the issue when there is very frequent update/write on tbl_mandrill_email_mapping table. It's a log table which handles the email event's(sent,open,click,bounce etc.. log). In my case this table gets very busy once any bulk email sent out(approx 1,00,000 email) and it start handling all above events, sometimes this events are very frequent, In this case all the queries which reference the tbl_mandrill_email_mapping get stuck.
Any suggestion to handle this kind of situation? I am thinking about read replica so that all update/insert will handle by different server and read handle by different server, Will it work? Or is there any batter solution to handle this situation?
I am open for any other suggestion, Please suggest.
No! Do not break big tables into smaller ones. You already may have "too many tables". Instead, focus on indexing. Let's see SHOW CREATE TABLE and some of the important queries.
A table growing in size will slow down queries that fail to make good use of indexes. Not so for good indexes. (There are exceptions; let's see your queries.)
You have only 4GB? And you are shoehorning a 3GB buffer_pool into it? That does not leave much room for other structure, nor for other applications. This can lead to swapping, which is terrible for performance. Drop innodb_buffer_pool_size to 1500M.
Increasing the 'machine' size (and buffer_pool) may or may not help. The buffer_pool is a "cache", so the 40GB is not totally relevant. What we need is the "working set size", for which there is no good metric.
Do you know if you are I/O-bound? Or CPU-bound?
Based on the title, I recommend discussing the query.
More
(Now looking at Schema)
More indexes --> slower loading. Normally, a few good indexes is well worth the extra load time. But the emphasis is on few. Do not index flags; they will almost never be useful. (I assume your is_* columns are flags?)
Does your app really care when each column was updated? I see apps that start with such, but eventually abandon the idea.
Be careful not to mix CHARACTER SETs or COLLATIONs when JOINing. That is, in FROM a JOIN b ON a.x = b.y if x and y are no the same CHARACTER SET and COLLATION, then no index will be used.
Query 1 would benefit from this composite (and covering) index: INDEX(log_id, email, open).
I don't see the CREATE TABLE tbl_contact_173_230??

Unknown column 'id' in 'field list' even though this field is nowhere in table or query

I am trying to run the following insert:
insert into li_appointments.li_appointments
(`app_datetime`, `app_facility`,
`app_department`, `app_address`,
`app_language`, `app_requesting_person`,
`app_service_provider`, `app_client_id`,
`app_client_other`, `app_medicaid_status`,
`les_name`, `les_dob`, `les_medicaid_id`,
`app_notes`, `created_by`, `created_on`,
`app_callback_num`, `uofu_csn` )
Values(cast(concat(str_to_date('02/28/2014', '%m/%d/%Y')
, ' ', '09:45') as datetime), 'testt', 'test', 'test'
, 'test' , 'test' , 'test', '1094', 'test', '1'
, AES_ENCRYPT('****', 'test'), '10/10/1000', AES_ENCRYPT('****', 'test')
, 'test', 'admin', now(), 'testtest', '')
And I get this error every time:
Unknown column 'id' in 'field list'
Even though there is no column 'id' anywhere. This query worked perfectly yesterday so I am at a loss.
This is the table we are trying to edit:
CREATE TABLE `li_appointments` (
`app_id` int(11) NOT NULL AUTO_INCREMENT,
`app_datetime` datetime DEFAULT NULL,
`app_facility` varchar(200) DEFAULT NULL,
`app_department` varchar(200) DEFAULT NULL,
`app_address` varchar(200) DEFAULT NULL,
`app_language` varchar(200) DEFAULT NULL,
`app_requesting_person` varchar(200) DEFAULT NULL,
`app_service_provider` varchar(200) DEFAULT NULL,
`app_client_id` int(11) DEFAULT NULL,
`app_client_other` varchar(200) DEFAULT NULL,
`app_medicaid_status` int(11) DEFAULT NULL,
`app_health_program` varchar(50) DEFAULT NULL,
`les_name` varbinary(500) DEFAULT NULL,
`les_dob` varchar(25) DEFAULT NULL,
`les_medicaid_id` varbinary(500) DEFAULT NULL,
`billing_total_time` time DEFAULT NULL,
`billing_workorder_received` int(11) DEFAULT NULL,
`billing_admin_fee` float DEFAULT NULL,
`billing_notes` varchar(1000) DEFAULT NULL,
`app_notes` varchar(1000) DEFAULT NULL,
`created_by` varchar(100) DEFAULT NULL,
`created_on` datetime DEFAULT NULL,
`modified_by` varchar(100) DEFAULT NULL,
`modified_on` datetime DEFAULT NULL,
`wo_entered_by` varchar(100) DEFAULT NULL,
`app_callback_num` varchar(45) DEFAULT NULL,
`terp_id` varchar(45) DEFAULT NULL,
`app_covered_by` varchar(45) DEFAULT NULL,
`covered_on` datetime DEFAULT NULL,
`uofu_csn` varchar(45) DEFAULT NULL,
PRIMARY KEY (`app_id`),
KEY `created_By` (`created_by`),
KEY `index2` (`app_id`,`app_datetime`,`terp_id`,`app_language`),
KEY `Extra` (`app_id`,`app_datetime`,`app_client_id`)
) ENGINE=MyISAM AUTO_INCREMENT=515394839 DEFAULT CHARSET=utf8