is there any methods to optimize a slow running query? - mysql

we are getting frequent slow log alerts because of below query.this query using indexes
i was tried to optimize this query but no luck support me to optimize slow query and how to improve the performance.
i want the output in best time.
My query is
select t.row_mod,
t.widget_info_id,
t.widget_name,
t.text1,
m.pretty_name,
w.website_id,
w.fol3_website_id
from fbw_info_text t,
fbw_info_meta m,
fol3_website w
where t.widget_info_id = m.widget_info_id
and t.widget_name = m.widget_name
and (
m.widget_type = 'checkbox'
or t.widget_name in (
'ccapi_approved_transactions_msg',
'ccapi_declined_transactions_msg',
'ccapi_failed_transactions_msg'
)
)
and t.website_id = w.website_id
and (
(t.row_mod >= '2013-10-24 20:57:32'
and t.row_mod <= '2013-10-24 20:59:32')
or
(m.row_mod >= '2013-10-24 20:57:32'
and m.row_mod <= '2013-10-24 20:59:32')
)
order by widget_info_id, widget_name;
Explain Plan:
+----+-------------+-------+-------+-------------------------------------+----------+---------+----------------------------------------+-------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-------------------------------------+----------+---------+----------------------------------------+-------+----------------------------------------------+
| 1 | SIMPLE | w | index | idx_1055,idx_1786 | idx_1522 | 4 | NULL | 25953 | Using index; Using temporary; Using filesort |
| 1 | SIMPLE | t | ref | idx_4639,idx_1055,idx_2718,idx_3388 | idx_1055 | 53 | cms.w.website_id | 12 | Using where |
| 1 | SIMPLE | m | ref | idx_2718 | idx_2718 | 105 | cms.t.widget_name,cms.t.widget_info_id | 1 | Using where |
+----+-------------+-------+-------+-------------------------------------+----------+---------+----------------------------------------+-------+----------------------------------------------+
3 rows in set (0.01 sec)
Table structures
mysql> show create table fbw_info_text\G
*************************** 1. row ***************************
Table: fbw_info_text
Create Table: CREATE TABLE `fbw_info_text` (
`row_mod` datetime DEFAULT NULL,
`row_create` datetime DEFAULT NULL,
`widget_info_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`widget_name` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`website_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`text1` longtext COLLATE latin1_bin,
`text2` longtext COLLATE latin1_bin,
`submitted_user_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`use_default` varchar(1) COLLATE latin1_bin DEFAULT NULL,
`active_flag` varchar(1) COLLATE latin1_bin DEFAULT NULL,
`status` varchar(1) COLLATE latin1_bin DEFAULT NULL,
UNIQUE KEY `idx_4639` (`website_id`,`widget_info_id`,`widget_name`,`status`),
KEY `idx_1055` (`website_id`),
KEY `idx_2718` (`widget_info_id`,`widget_name`),
KEY `idx_3388` (`widget_info_id`,`website_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.01 sec)
mysql> show create table fbw_info_meta\G
*************************** 1. row ***************************
Table: fbw_info_meta
Create Table: CREATE TABLE `fbw_info_meta` (
`row_mod` datetime DEFAULT NULL,
`row_create` datetime DEFAULT NULL,
`widget_name` varchar(50) COLLATE latin1_bin NOT NULL,
`pretty_name` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`widget_info_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`widget_type` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`needs_approval` varchar(1) COLLATE latin1_bin DEFAULT NULL,
`widget_value` longtext COLLATE latin1_bin,
`widget_order` int(11) DEFAULT NULL,
`widget_attributes` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`capability_name` varchar(30) COLLATE latin1_bin DEFAULT NULL,
`design_type` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`on_approval_hook` longtext COLLATE latin1_bin,
`display_hook` longtext COLLATE latin1_bin,
`status` varchar(1) COLLATE latin1_bin DEFAULT NULL,
`needs_archive` varchar(1) COLLATE latin1_bin DEFAULT NULL,
UNIQUE KEY `idx_2718` (`widget_name`,`widget_info_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)
mysql> show create table fol3_website\G
*************************** 1. row ***************************
Table: fol3_website
Create Table: CREATE TABLE `fol3_website` (
`row_mod` datetime DEFAULT NULL,
`row_create` datetime DEFAULT NULL,
`fol3_website_id` int(11) NOT NULL,
`website_id` varchar(50) COLLATE latin1_bin NOT NULL,
`activate_default_host` int(11) DEFAULT '0',
`active` int(11) NOT NULL DEFAULT '1',
`advance_notice_days` int(11) DEFAULT '0',
`catrequest` int(11) DEFAULT '0',
`chain` int(11) DEFAULT NULL,
`color_scheme` varchar(30) COLLATE latin1_bin DEFAULT NULL,
`conversion_rate` int(11) DEFAULT '0',
`comment` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`creation_date` datetime DEFAULT NULL,
`defunct_2` varchar(5) COLLATE latin1_bin DEFAULT NULL,
`default_website` varchar(50) COLLATE latin1_bin NOT NULL,
`designtype` varchar(25) COLLATE latin1_bin DEFAULT NULL,
`key_contact_name` varchar(55) COLLATE latin1_bin DEFAULT NULL,
`key_contact_phone` varchar(60) COLLATE latin1_bin DEFAULT NULL,
`language_id` varchar(5) COLLATE latin1_bin DEFAULT NULL,
`defunct_0` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`mercury` int(11) NOT NULL DEFAULT '1',
`mgnt_defaults_last_applied` datetime DEFAULT NULL,
`rank` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`return_url` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`defunct_1` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`store_id` varchar(15) COLLATE latin1_bin NOT NULL,
`defunct_3` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`display_website_url` varchar(25) COLLATE latin1_bin DEFAULT NULL,
`webloyalty` int(11) DEFAULT NULL,
`name` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`transmission_type` int(11) DEFAULT NULL,
`segment` int(11) DEFAULT NULL,
`site_id` varchar(25) COLLATE latin1_bin DEFAULT NULL,
`canonical_status` int(1) DEFAULT '0',
`canonical_domain` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`mobile_site_active` tinyint(1) DEFAULT '1',
UNIQUE KEY `idx_1055` (`website_id`),
UNIQUE KEY `idx_1522` (`fol3_website_id`),
UNIQUE KEY `idx_1786` (`website_id`,`active`),
KEY `idx_1867` (`transmission_type`),
KEY `idx_857` (`store_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)

You can try SHOW Profiling functionality of MySQL to find out where most of the time has been taken by a query execution and optimize based on result:
set profiling=1;
Run your slow query (eg SELECT * FROM table WHERE name='xxx';
SHOW PROFILES;
http://dev.mysql.com/doc/refman/5.0/en/show-profile.html
Analyze EXPLAIN EXTENED output and optimize query accourdingly and profile the query again to see gain in performance.

Related

How do I improve this SQL query?

I have the following query:
select
`stories`.*
from
`stories`
inner join `communities` on `stories`.`community_id` = `communities`.`id`
inner join `communities_followers` on `communities`.`id` = `communities_followers`.`community_id`
where
`is_published` = 1
and `communities_followers`.`user_id` = 1
and `communities_followers`.`status` = 1
order by
`stories`.`created_at` desc
limit
20 offset 0
With an single index on communities_followers.user_id and a compound index on ['user_id', 'status']
When doing an explain on the query, this is the result:
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------------------+------------+--------+--------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------+---------+---------------------------------------------+------+----------+----------------------------------------------+
| 1 | SIMPLE | communities_followers | NULL | ref | communities_followers_user_id_index,communities_followers_community_id_index,communities_followers_community_id_user_id_status_index | communities_followers_user_id_index | 4 | const | 77 | 10.00 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | communities | NULL | eq_ref | PRIMARY,communities_id_default_community_index | PRIMARY | 4 | grepless.communities_followers.community_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | stories | NULL | ref | stories_community_id_index | stories_community_id_index | 4 | grepless.communities_followers.community_id | 3968 | 100.00 | NULL |
+----+-------------+-----------------------+------------+--------+--------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------+---------+---------------------------------------------+------+----------+----------------------------------------------+
Explain
What else can I do to improve this? communities_followers does contain a ton of records.
Stories table:
CREATE TABLE `stories` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`user_id` int unsigned NOT NULL,
`publisher_id` int unsigned DEFAULT NULL,
`community_id` int unsigned NOT NULL,
`content_type_id` int unsigned NOT NULL DEFAULT '10',
`title` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`description` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`meta_description` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`score` int NOT NULL DEFAULT '0',
`score_alternate` int NOT NULL DEFAULT '0',
`views` int NOT NULL DEFAULT '0',
`url` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`source_url` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`picture` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`meta` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`embed` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`picture_original` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`picture_huge` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`picture_big` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`picture_small` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`picture_extra` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`slug` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`is_published` tinyint(1) NOT NULL DEFAULT '1',
`is_summarized` tinyint(1) NOT NULL DEFAULT '0',
`language` varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`show_in_feed` tinyint(1) NOT NULL DEFAULT '1',
`is_pinned` tinyint(1) NOT NULL DEFAULT '0',
`has_pictures_localized` tinyint(1) NOT NULL DEFAULT '0',
`has_pictures_optimized` tinyint(1) NOT NULL DEFAULT '0',
`has_audio` tinyint(1) NOT NULL DEFAULT '0',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`author` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`comments_count` bigint NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `stories_created_at_index` (`created_at`),
KEY `stories_user_id_index` (`user_id`),
KEY `stories_community_id_index` (`community_id`),
KEY `stories_content_type_id_index` (`content_type_id`),
KEY `stories_score_index` (`score`),
KEY `stories_slug_index` (`slug`),
KEY `stories_show_in_feed_index` (`show_in_feed`),
KEY `stories_publisher_id_index` (`id`),
KEY `stories_deleted_at_is_published_content_type_id_index` (`deleted_at`,`is_published`,`content_type_id`),
KEY `stories_publisher_id_deleted_at_index` (`publisher_id`,`deleted_at`),
KEY `stories_is_published_deleted_at_index` (`is_published`,`deleted_at`,`community_id`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=952978 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
communities table
communities | CREATE TABLE `communities` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`user_id` int unsigned DEFAULT NULL,
`name` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`slug` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`header` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`color` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`background` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`background_cover` tinyint(1) NOT NULL DEFAULT '1',
`picture_big` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`picture_small` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`has_pictures_optimized` tinyint(1) NOT NULL DEFAULT '0',
`default_community` tinyint(1) NOT NULL DEFAULT '0',
`is_popular` tinyint(1) NOT NULL DEFAULT '0',
`status` smallint NOT NULL DEFAULT '1',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`stories_count` bigint NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `communities_user_id_index` (`user_id`),
KEY `communities_name_index` (`name`),
KEY `communities_slug_index` (`slug`),
KEY `communities_status_index` (`status`),
KEY `communities_default_community_index` (`default_community`),
KEY `communities_id_default_community_index` (`id`,`default_community`)
) ENGINE=MyISAM AUTO_INCREMENT=183 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
Communities followers table
communities_followers | CREATE TABLE `communities_followers` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`user_id` int unsigned NOT NULL,
`community_id` int unsigned NOT NULL,
`status` smallint NOT NULL DEFAULT '0',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `communities_followers_user_id_index` (`user_id`),
KEY `communities_followers_community_id_index` (`community_id`),
KEY `communities_followers_community_id_user_id_status_index` (`community_id`,`user_id`,`status`)
) ENGINE=MyISAM AUTO_INCREMENT=326484 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
SELECT
`stories`.*
FROM
`communities_followers`
INNER JOIN
`stories`
ON `stories`. `community_id` = `communities_followers`.`community_id`
WHERE
`communities_followers`.`user_id` = 1
AND `communities_followers`.`status` = 1
AND `stories`.`is_published` = 1
ORDER BY
`stories`.`created_at` DESC
LIMIT
20 OFFSET 0
Then to speed up the initial filter index...
communities_followers(user_id, status, community_id)
Then to speed up the join AND the ordering and limit...
stories(community_id, is_published, created_at)
OR
stories(is_published, created_at, community_id)
But I'd use EXISTS, as it will still work if you query against multiple users (without causing duplication in the results)...
SELECT
`stories`.*
FROM
`stories`
WHERE
`stories`.`is_published` = 1
AND EXISTS (
SELECT *
FROM `communities_followers`
WHERE `communities_followers`.`user_id` = 1
AND `communities_followers`.`status` = 1
AND `communities_followers`.`community_id` = `stories`.`community_id`
)
ORDER BY
`stories`.`created_at` DESC
LIMIT
20 OFFSET 0

how to optimize the below query without adding index

the below query has some problems so can we able to optimize the query with the current index format cuz the table size is huge so I don't know what to do.
Query:
select
`sale_amount`,
`provider_transaction_id`,
`id` as `acc_id`,
`status`,
`service_id`,
`provider_status`,
`is_sale_settled`,
CASE WHEN status = 1
and is_sale_settled = 1 then 4 WHEN status = 1
and is_sale_settled = 0 then 3 ELSE 5 END AS acc_ics_status,
`system_transaction_id`,
`engine_transaction_id`,
`transaction_date`,
`created_at` as `transaction_date_time`,
HOUR(created_at) as transaction_hour
from
`st_ret_txn_aeps`
where
`created_at` >= '2022-10-03 14:45:01'
and `created_at` <= '2022-10-03 14:48:00'
and `service_id` = 14
and `transaction_date` = '2022-10-03'
Row scans;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: st_ret_txn_aeps
partitions: NULL
type: index_merge
possible_keys: transaction_id_sevrice_id_unique,st_ret_txn_aeps_service_id_index,idx_txn_date
key: idx_txn_date,st_ret_txn_aeps_service_id_index
key_len: 3,1
ref: NULL
rows: 122328
filtered: 11.11
Extra: Using intersect(idx_txn_date,st_ret_txn_aeps_service_id_index); Using where
1 row in set, 1 warning (0.00 sec)
Table structure;
*************************** 1. row ***************************
Table: st_ret_txn_aeps
Create Table: CREATE TABLE `st_ret_txn_aeps` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`service_id` tinyint(4) NOT NULL,
`transaction_type_id` tinyint(4) NOT NULL,
`business_org_id` int(10) unsigned NOT NULL,
`sub_agent_id` int(11) DEFAULT NULL,
`agent_id` int(10) unsigned DEFAULT NULL,
`business_org_user_id` int(10) unsigned NOT NULL,
`business_org_sub_user_id` int(10) unsigned DEFAULT NULL,
`retailer_category_code` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`rate_profile_id` int(11) DEFAULT '0',
`wallet_type` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`business_org_pan` varchar(12) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`business_org_gstin` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`operator_id` int(10) unsigned NOT NULL,
`operator_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`product_id` int(11) DEFAULT NULL,
`sub_product_id` int(11) DEFAULT NULL,
`provider_id` int(10) unsigned NOT NULL,
`provider_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`branch_id` int(11) NOT NULL DEFAULT '0',
`branch_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`sale_amount` double NOT NULL,
`total_b_org_charges` double NOT NULL,
`total_b_org_comm_tds` double DEFAULT '0',
`total_b_org_commission` double NOT NULL,
`final_debit_value` double NOT NULL,
`balance_after_txn` double NOT NULL,
`system_transaction_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`engine_transaction_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`provider_transaction_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`operator_transaction_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reference_1` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reference_2` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reference_3` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reference_4` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reference_5` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`business_org_markup` double NOT NULL DEFAULT '0',
`refund_date` date DEFAULT NULL,
`remark` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`is_invoice_data_generated` tinyint(1) NOT NULL,
`status` tinyint(4) NOT NULL COMMENT '1: success ,2: failed',
`provider_status` tinyint(4) DEFAULT NULL,
`provider_status_old` tinyint(4) DEFAULT NULL,
`transaction_date` date NOT NULL,
`customer_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`customer_mobile_no` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`app_type` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '1:WEBPOS 2:ANDROID',
`is_cashout_sale` tinyint(1) NOT NULL,
`is_sale_settled` tinyint(1) NOT NULL,
`settlement_date` date DEFAULT NULL,
`analytics_sync_status` tinyint(1) DEFAULT NULL,
`analytics_sync_status_refund` tinyint(1) DEFAULT NULL,
`invoice_data_id` int(11) DEFAULT NULL,
`credit_note_data_id` int(11) DEFAULT NULL,
`status_updated_at` bigint(20) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `transaction_id_sevrice_id_unique` (`service_id`,`engine_transaction_id`),
UNIQUE KEY `system_transaction_id_unique` (`system_transaction_id`),
KEY `st_ret_txn_aeps_service_id_index` (`service_id`),
KEY `st_ret_txn_aeps_business_org_id_index` (`business_org_id`),
KEY `st_ret_txn_aeps_agent_id_index` (`agent_id`),
KEY `st_ret_txn_aeps_business_org_user_id_index` (`business_org_user_id`),
KEY `st_ret_txn_aeps_system_transaction_id_index` (`system_transaction_id`),
KEY `st_ret_txn_aeps_refund_date_index` (`refund_date`),
KEY `idx_txn_date` (`transaction_date`),
KEY `st_ret_txn_aeps_settlement_date_index` (`settlement_date`),
KEY `status_updated_at_idx` (`status_updated_at`)
) ENGINE=InnoDB AUTO_INCREMENT=122756253 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
yeah I know if we create an index for the created_at column means the problem will solve does this have any other way to optimize?
Well, you could delete all the data, then the query would be very quick (just kidding)!
Honestly, you need to create the index to optimize this query. The best index for this query would be a compound index on (service_id, transaction_date, created_at) in that order.
There are at least three ways to create the index without much downtime:
pt-online-schema-change
gh-ost
Create the index on a replica database, then when the index is ready, switch the app to use the replica
At my last job we used pt-online-schema-change. While I worked there we executed over 75,000 ALTER TABLEs in production during peak hours, without downtime. Some of the tables were larger than yours.

How to change collation for view table in mysql

When I run this query:
select b.email,
concat('[AAA]: Xin chao QK ', a.client_name, ' HD ',a.account_no, ' vua khoi tao') as subject,
replace( (select email_content from xxff_message where refid=1 and status='O'),'#LOAN_ID#', a.account_no) as message, -- cp init
a.loan_id
from interface.vw_loan_full a
left join interface.xxff_client b
on a.client_id=b.client_id
where 1=1
and a.status=100
and (a.loan_id, email) not in ( select loan_id, receiver from xxff_loan_message_sent where stage='INIT100_MSG_EMAIL')
and date(a.value_date) between DATE_ADD(CURDATE(), INTERVAL -30 day) and date(now());
And I get this error:
Err] 1270 - Illegal mix of collations (utf8_unicode_ci,IMPLICIT),
(utf8_unicode_ci,COERCIBLE), (utf8_general_ci,IMPLICIT) for operation
'replace'
When I research, this cause is different utf8_unicode_ci and utf8_general_ci. But I checked all table, database are using utf8_unicode_ci .
But view table vw_loan_full is utf8_general_ci. Please help me change collation of view table.
Because I run command:
ALTER TABLE vw_loan_full CONVERT TO CHARACTER SET utf8 COLLATE
utf8_unicode_ci;
And get Error:
[Err] 1347 - 'interface.vw_loan_full' is not BASE TABLE
My result from SHOW CREATE TABLE xxff_client:
CREATE TABLE xxff_client ( client_id bigint(20) NOT NULL,
client_name varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
national_id varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
issue_date date DEFAULT NULL, issue_place varchar(120) COLLATE
utf8_unicode_ci DEFAULT NULL, Gender varchar(15) COLLATE
utf8_unicode_ci DEFAULT NULL, dob date DEFAULT NULL, phone
varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, email
varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL, address
varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL, status
varchar(1) COLLATE utf8_unicode_ci DEFAULT NULL, creation_date
date DEFAULT NULL, USER_ID varchar(20) COLLATE utf8_unicode_ci
DEFAULT NULL, company_name varchar(150) COLLATE utf8_unicode_ci
DEFAULT NULL, company_address varchar(255) COLLATE utf8_unicode_ci
DEFAULT NULL, title varchar(50) COLLATE utf8_unicode_ci DEFAULT
NULL, dept_name varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
join_date date DEFAULT NULL, salary_date int(11) DEFAULT NULL,
remain_of_laborcontract int(11) DEFAULT NULL, salary_recently
varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL, eco_userid
bigint(20) DEFAULT NULL, otp_count tinyint(20) DEFAULT '0',
PRIMARY KEY (client_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC
My result from SHOW CREATE TABLE xxff_loan_message_sent:
CREATE TABLE xxff_loan_message_sent ( refid bigint(20) NOT NULL
AUTO_INCREMENT, loan_id int(11) NOT NULL, stage varchar(20)
COLLATE utf8_unicode_ci DEFAULT NULL, sent_status varchar(6)
COLLATE utf8_unicode_ci DEFAULT NULL, created_by varchar(15)
COLLATE utf8_unicode_ci DEFAULT NULL, created_date date DEFAULT
NULL, receiver varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (refid) ) ENGINE=InnoDB AUTO_INCREMENT=280 DEFAULT
CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC
My result from SHOW CREATE TABLE vw_loan_full:
CREATE ALGORITHM=UNDEFINED DEFINER=root#% SQL SECURITY DEFINER
VIEW vw_loan_full AS select a.id AS loan_id,a.client_id AS
client_id,b.display_name AS client_name,b.account_no AS
cif_no,a.product_id AS product_id,c.short_name AS
product_name,a.currency_code AS
ccy_code,a.annual_nominal_interest_rate AS
rate,a.term_frequency AS term,a.approved_principal AS
approved_principal,a.principal_amount AS
disbursed_amount,a.principal_outstanding_derived AS
pos,a.loan_status_id AS status,a.submittedon_date AS
created_date,a.submittedon_userid AS
created_by,a.expected_disbursedon_date AS
value_date,a.maturedon_date AS
maturity_date,a.approvedon_date AS
approvedon_date,a.disbursedon_date AS
disbursedon_date,a.expected_maturedon_date AS
expected_maturedon_date,a.loanpurpose_cv_id AS
loan_purpose,a.account_no AS account_no from
((mifostenant-default.m_loan a left join
mifostenant-default.m_client b on((a.client_id = b.id)))
left join mifostenant-default.m_product_loan c
on((a.product_id = c.id)))
My result from SHOW CREATE TABLE vw_loan_full:
CREATE TABLE xxff_message ( refid bigint(20) NOT NULL
AUTO_INCREMENT, msg_type varchar(8) COLLATE utf8_unicode_ci
DEFAULT NULL, sms_content varchar(500) COLLATE utf8_unicode_ci
DEFAULT NULL, email_content varchar(2000) COLLATE utf8_unicode_ci
DEFAULT NULL, email_subject varchar(250) COLLATE utf8_unicode_ci
DEFAULT NULL, status varchar(4) COLLATE utf8_unicode_ci DEFAULT
NULL, created_by varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
created_date datetime DEFAULT NULL, updated_by varchar(15)
COLLATE utf8_unicode_ci DEFAULT NULL, updated_date datetime
DEFAULT NULL, stage_code varchar(35) COLLATE utf8_unicode_ci
DEFAULT NULL, email_receiver varchar(120) COLLATE utf8_unicode_ci
DEFAULT NULL, PRIMARY KEY (refid) ) ENGINE=InnoDB
AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
ROW_FORMAT=DYNAMIC
How I can change collation for view table in MySQL? Thanks for your helpfull !!!
MySQL 5.5 in Windows

how to improve performance of my query?

we have written a query like below and also created proper indexes on table.
QUERY
SELECT ref_order_id, order_id, cams_ref_order_id
FROM cart_entries_archive
WHERE regular_price <> product_price
AND ref_order_id >0
AND cams_ref_order_id > 0;
But the query is performing full table scan due to this we are getting load spikes .
we tried by adding indexes on where clause columns but still performing full scan .Please rewrite the query if possible.
query explain plan
mysql> explain select ref_order_id,order_id,cams_ref_order_id from cart_entries_archive where regular_price <> product_price and ref_order_id >0 and cams_ref_order_id > 0;
+----+-------------+----------------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | cart_entries_archive | ALL | NULL | NULL | NULL | NULL | 6490560 | Using where |
+----+-------------+----------------------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)
Table structure:
mysql> show create table cart_entries_archive\G
*************************** 1. row ***************************
Table: cart_entries_archive
Create Table: CREATE TABLE `cart_entries_archive` (
`row_mod` datetime DEFAULT NULL,
`row_create` datetime DEFAULT NULL,
`address_id` int(11) DEFAULT NULL,
`backorder_date` datetime DEFAULT NULL,
`cancelled_date` datetime DEFAULT NULL,
`cart_entry_id` int(11) NOT NULL,
`cart_id` int(11) NOT NULL,
`delivery_date` datetime DEFAULT NULL,
`delivery_method` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`delivery_note` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`discount_amount` decimal(8,2) DEFAULT '0.00',
`gift_message` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`occasion` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`order_date` datetime DEFAULT NULL,
`order_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`order_status` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`product_name` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`product_extra` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`product_price` decimal(8,2) DEFAULT '0.00',
`product_count` int(11) DEFAULT NULL,
`product_cost` decimal(8,2) DEFAULT '0.00',
`product_sku` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`product_notes` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`parent_product_sku` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`returned_date` datetime DEFAULT NULL,
`release_date` datetime DEFAULT NULL,
`regular_price` decimal(8,2) DEFAULT '0.00',
`shipping_cost` decimal(8,2) DEFAULT '0.00',
`service_charge` decimal(8,2) DEFAULT '0.00',
`sku_option_name` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`sku_option_value` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_company` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_fname` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_lname` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_address` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`shipping_address2` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`shipping_city` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_country` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_province` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`shipping_postal_code` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`shipping_phone` varchar(20) COLLATE latin1_bin DEFAULT NULL,
`shipping_phone_ext` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`ship_tracking_number` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`status` varchar(20) COLLATE latin1_bin DEFAULT NULL,
`tax` decimal(8,2) DEFAULT '0.00',
`total_charge` decimal(8,2) DEFAULT '0.00',
`website_id` int(11) DEFAULT NULL,
`microsite_id` int(11) DEFAULT NULL,
`defer_reason` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`defer_key` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`shipping_evening_phone` varchar(20) COLLATE latin1_bin DEFAULT NULL,
`shipping_mobile_phone` varchar(20) COLLATE latin1_bin DEFAULT NULL,
`shipping_email` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`shipping_title` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`shipping_district` varchar(30) COLLATE latin1_bin DEFAULT NULL,
`location_type` varchar(20) COLLATE latin1_bin DEFAULT NULL,
`occasion_id` int(11) DEFAULT NULL,
`occasion_date` datetime DEFAULT NULL,
`occasion_do_remind` int(11) DEFAULT NULL,
`coupon_ref_source_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`delivery_date_verified` int(11) DEFAULT NULL,
`florist_peak_charge` float DEFAULT NULL,
`member_id` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`delivery_location_code` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`simply_iflora` int(11) DEFAULT NULL,
`rotation_weight` int(11) DEFAULT NULL,
`area_charge` decimal(8,2) DEFAULT NULL,
`cf_indicator` varchar(5) COLLATE latin1_bin DEFAULT NULL,
`category_id` varchar(10) COLLATE latin1_bin DEFAULT NULL,
`cms_note` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`qas_queried` int(11) DEFAULT NULL,
`shipping_verified` int(11) DEFAULT NULL,
`occasion_event_name` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`push_date` datetime DEFAULT NULL,
`ref_order_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`relationship` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`std_delivery_id` int(11) DEFAULT NULL,
`opt_delivery_id` int(11) DEFAULT NULL,
`service_date` datetime DEFAULT NULL,
`parameters` varchar(1024) COLLATE latin1_bin DEFAULT NULL,
`order_type` varchar(32) COLLATE latin1_bin DEFAULT NULL,
`cams_ref_order_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`membership_discount` decimal(8,2) DEFAULT NULL,
PRIMARY KEY (`cart_entry_id`),
UNIQUE KEY `idx_2204` (`cart_entry_id`,`cart_id`),
UNIQUE KEY `idx_2318` (`cart_entry_id`,`order_id`),
KEY `idx_1049` (`order_date`),
KEY `idx_726` (`cart_id`),
KEY `idx_840` (`order_id`),
KEY `idx_row_create` (`row_create`),
KEY `idx_1035` (`push_date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)
Your where clause uses ">" rather than "=", making it far less likely that an index will be useful. What % of rows in that table meet the criteria where ref_order_id>0 and cams_ref_order_id>0 ? If it is a high percentage, a table scan could be the fastest approach. Even if 10% of the records meet that criteria, it might mean the RDBMS has to read almost every page of the table.
If you want it to be "Index Only", you could add the following index:
create index TMP001 on cart_entries_archive
(ref_order_id, cams_ref_order_id, order_id, regular_price, product_price)
The fields from the where clause lead the index and everything else follows. If this is the only query you care about, and if the cost of maintaining the index is negligible, then create it and you're done.

My query is running more than 4 seconds help me here to get result in less than 1 second

My query is running more than 4 seconds.
i want result in less than 1 second
help me here.
Query:
SELECT a.index_id,
title
FROM fb_indices a,
fb_catalog_indices b FORCE INDEX (idx_1986)
WHERE a.index_id = b.index_id
AND type = 'international'
AND title IS NOT NULL
Explain Plan:
mysql> explain select a.index_id,title from fb_indices a, fb_catalog_indices b force index (idx_1986) where a.index_id = b.index_id and type='international' and title is not NULL;
+----+-------------+-------+------+-----------------+----------+---------+----------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+-----------------+----------+---------+----------------+------+-------------+
| 1 | SIMPLE | a | ref | idx_836,idx_450 | idx_450 | 53 | const | 79 | Using where |
| 1 | SIMPLE | b | ref | idx_1986 | idx_1986 | 103 | cms.a.index_id | 590 | Using where |
+----+-------------+-------+------+-----------------+----------+---------+----------------+------+-------------+
2 rows in set (0.00 sec)
Structure:
mysql> show create table fb_indices\G
*************************** 1. row ***************************
Table: fb_indices
Create Table: CREATE TABLE `fb_indices` (
`row_mod` datetime DEFAULT NULL,
`row_create` datetime DEFAULT NULL,
`index_id` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`description` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`country_id` varchar(2) COLLATE latin1_bin DEFAULT NULL,
`index_level` int(11) DEFAULT NULL,
`type` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`approval_flag` int(11) DEFAULT NULL,
`username` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`search_product_type` int(11) DEFAULT NULL,
`search_color` int(11) DEFAULT NULL,
`search_price` int(11) DEFAULT NULL,
UNIQUE KEY `idx_836` (`index_id`),
KEY `idx_450` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)
mysql> show create table fb_catalog_indices\G
*************************** 1. row ***************************
Table: fb_catalog_indices
Create Table: CREATE TABLE `fb_catalog_indices` (
`row_mod` datetime DEFAULT NULL,
`row_create` datetime DEFAULT NULL,
`index_id` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`website_id` varchar(50) COLLATE latin1_bin DEFAULT NULL,
`title` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`subheading` longtext COLLATE latin1_bin,
`product_ids` longtext COLLATE latin1_bin,
`sub_index_ids` longtext COLLATE latin1_bin,
`mapped_index_ids` longtext COLLATE latin1_bin,
`username` varchar(100) COLLATE latin1_bin DEFAULT NULL,
`content` longtext COLLATE latin1_bin,
`meta_description` longtext COLLATE latin1_bin,
`meta_tag_keyword` longtext COLLATE latin1_bin,
`mapped_link_text` longtext COLLATE latin1_bin,
`mapped_alt_tags` longtext COLLATE latin1_bin,
`long_title` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`html_content` longtext COLLATE latin1_bin,
`use_html_content` int(11) DEFAULT '0',
`flash_size` longtext COLLATE latin1_bin,
`custom_index_ids` longtext COLLATE latin1_bin,
`html_header_1` longtext COLLATE latin1_bin,
`html_header_2` longtext COLLATE latin1_bin,
`search_product_type` int(11) DEFAULT NULL,
`search_color` int(11) DEFAULT NULL,
`search_price` int(11) DEFAULT NULL,
`disable_rankings` int(11) DEFAULT NULL,
`html_footer_1` longtext COLLATE latin1_bin,
`dramashot_html` longtext COLLATE latin1_bin,
`quickshop_image` longtext COLLATE latin1_bin,
`sort_by_price_css` longtext COLLATE latin1_bin,
`pagination_css` longtext COLLATE latin1_bin,
`suppress_navigation` int(11) DEFAULT NULL,
`suppress_quickshop_image` int(11) DEFAULT NULL,
`mvt_control_script` longtext COLLATE latin1_bin,
`mvt_tracking_script` longtext COLLATE latin1_bin,
`enable_gbb_mouse` int(11) DEFAULT NULL,
`enable_gbb_click` int(11) DEFAULT NULL,
`last_modified_time` datetime DEFAULT NULL,
`seo_footer_content` longtext COLLATE latin1_bin,
`alternate_gfi` int(11) DEFAULT NULL,
`zip_finder_html_content` longtext COLLATE latin1_bin,
`disable_quick_view_widget` int(11) DEFAULT NULL,
`indexpromo_title` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`indexpromo_style` longtext COLLATE latin1_bin,
`background_color` text COLLATE latin1_bin,
`sameday_text` text COLLATE latin1_bin,
`productdesc_text` text COLLATE latin1_bin,
`productprice_text` text COLLATE latin1_bin,
`gbbcatalog_text` text COLLATE latin1_bin,
`max_products_display` int(11) DEFAULT NULL,
UNIQUE KEY `idx_1986` (`index_id`,`website_id`),
KEY `idx_1055` (`website_id`),
KEY `idx_836` (`index_id`),
KEY `idx_1812` (`last_modified_time`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)
Create following index:
CREATE INDEX idx_999 ON fb_indices (index_id, type);
After that, this query is equivalent to yours and should work fast:
SELECT a.index_id,
b.title
FROM fb_indices a
JOIN fb_catalog_indices b ON (a.index_id = b.index_id)
WHERE a.type = 'international'
AND b.title IS NOT NULL
Tip: try not to force index usage - in your case, it was hurting. Instead, create an appropriate index.
SELECT a.index_id,
title
FROM fb_indices a,
(
SELECT
index_id
FROM
fb_catalog_indices
WHERE
type = 'international'
) as b
WHERE a.index_id = b.index_id
AND title IS NOT NULL