List temporary table columns in mysql - mysql

I have created one temporary table named "table1". I am trying to list the columns of my temp table. I am not getting any values. Here is my mysql query.
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table2';
Any body help me please?
Thanks

You cannot get the temporary table columns using the INFORMATION_SCHEMA
The only way which you can use is to go with SHOW CREATE TABLE table2
"INFORMATION_SCHEMA.COLUMNS" does not contains columns of temporary table.

You can use SHOW COLUMS to achieve this.
Example Table:
CREATE TEMPORARY TABLE SalesSummary (
product_name VARCHAR(50) NOT NULL,
total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00,
avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00,
total_units_sold INT UNSIGNED NOT NULL DEFAULT 0
);
Command:
SHOW COLUMNS FROM SalesSummary;
Outout:
mysql> SHOW COLUMNS FROM SalesSummary;
+------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| product_name | varchar(50) | NO | | NULL | |
| total_sales | decimal(12,2) | NO | | 0.00 | |
| avg_unit_price | decimal(7,2) | NO | | 0.00 | |
| total_units_sold | int(10) unsigned | NO | | 0 | |
+------------------+------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
More details are in the manual Section 13.7.5.5 for MySQL 5.7. Link
Another possibility is using SHOW CREATE TABLE:
mysql> SHOW CREATE TABLE SalesSummary\G
*************************** 1. row ***************************
Table: SalesSummary
Create Table: CREATE TEMPORARY TABLE `SalesSummary` (
`product_name` varchar(50) NOT NULL,
`total_sales` decimal(12,2) NOT NULL DEFAULT '0.00',
`avg_unit_price` decimal(7,2) NOT NULL DEFAULT '0.00',
`total_units_sold` int(10) unsigned NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
Here are some more details.
In Mysql 5.7 there is a seperate table called INNODB_TEMP_TABLE_INFO to achieve this.

It is possible to get a list of column names for any table.
This method will also work for TEMPORARY tables:
SET #columns_string = ''
;
SHOW
COLUMNS
FROM
`mysql`.`user`
WHERE
#columns_string := CONCAT(`Field`, ',', #columns_string)
;
SELECT #columns_string
;
#columns_string will have a value of:
account_locked,password_lifetime,password_last_changed,password_expired,authentication_string,plugin,max_user_connections,max_connections,max_updates,max_questions,x509_subject,x509_issuer,ssl_cipher,ssl_type,Create_tablespace_priv,Trigger_priv,Event_priv,Create_user_priv,Alter_routine_priv,Create_routine_priv,Show_view_priv,Create_view_priv,Repl_client_priv,Repl_slave_priv,Execute_priv,Lock_tables_priv,Create_tmp_table_priv,Super_priv,Show_db_priv,Alter_priv,Index_priv,References_priv,Grant_priv,File_priv,Process_priv,Shutdown_priv,Reload_priv,Drop_priv,Create_priv,Delete_priv,Update_priv,Insert_priv,Select_priv,User,Host,
The problem with this solution (besides needing to parse a string) is that the SHOW COLUMNS command will always generate an (empty) resultset.
This means that you can't really use this method in a stored procedure that returns a SELECT (you will end up with more than one resultset).

Related

MySQL - Create table with current date and return date that adjusts [duplicate]

Pretty straight forward question here, I think this should work but it doesn't. Why doesn't it?
CREATE TABLE INVOICE(
INVOICEDATE DATE NOT NULL DEFAULT CURRENT_DATE
)
It doesn't work because it's not supported
The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column
http://dev.mysql.com/doc/refman/5.5/en/create-table.html
According to this documentation, starting in MySQL 8.0.13, you will be able to specify:
CREATE TABLE INVOICE(
INVOICEDATE DATE DEFAULT (CURRENT_DATE)
)
MySQL 8.0.13 was released to General Availability in October 2018. The release info is located here.
declare your date column as NOT NULL, but without a default. Then add this trigger:
USE `ddb`;
DELIMITER $$
CREATE TRIGGER `default_date` BEFORE INSERT ON `dtable` FOR EACH ROW
if ( isnull(new.query_date) ) then
set new.query_date=curdate();
end if;
$$
delimiter ;
Currently from MySQL 8 you can set the following to a DATE column:
In MySQL Workbench, in the Default field next to the column, write: (curdate())
If you put just curdate() it will fail. You need the extra ( and ) at the beginning and end.
create table the_easy_way(
capture_ts DATETIME DEFAULT CURRENT_TIMESTAMP,
capture_dt DATE AS (DATE(capture_ts))
)
(MySQL 5.7)
I have the current latest version of MySQL: 8.0.20
So my table name is visit, my column name is curdate.
alter table visit modify curdate date not null default (current_date);
This writes the default date value with no timestamp.
----- 2016-07-04 MariaDB 10.2.1 -- Release Note -- -----
Support for DEFAULT with expressions (MDEV-10134).
----- 2018-10-22 8.0.13 General Availability -- -- -----
MySQL now supports use of expressions as default values in data type specifications. This includes the use of expressions as default values for the BLOB, TEXT, GEOMETRY, and JSON data types, which previously could not be assigned default values at all. For details, see Data Type Default Values.
As the other answer correctly notes, you cannot use dynamic functions as a default value. You could use TIMESTAMP with the CURRENT_TIMESTAMP attribute, but this is not always possible, for example if you want to keep both a creation and updated timestamp, and you'd need the only allowed TIMESTAMP column for the second.
In this case, use a trigger instead.
I came to this page with the same question in mind, but it worked for me!, Just thought to update here , may be helpful for someone later!!
MariaDB [niffdb]> desc invoice;
+---------+--------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------+------+-----+---------+----------------+
| inv_id | int(4) | NO | PRI | NULL | auto_increment |
| cust_id | int(4) | NO | MUL | NULL | |
| inv_dt | date | NO | | NULL | |
| smen_id | int(4) | NO | MUL | NULL | |
+---------+--------+------+-----+---------+----------------+
4 rows in set (0.003 sec)
MariaDB [niffdb]> ALTER TABLE invoice MODIFY inv_dt DATE NOT NULL DEFAULT (CURRENT_DATE);
Query OK, 0 rows affected (0.003 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [niffdb]> desc invoice;
+---------+--------+------+-----+-----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------+------+-----+-----------+----------------+
| inv_id | int(4) | NO | PRI | NULL | auto_increment |
| cust_id | int(4) | NO | MUL | NULL | |
| inv_dt | date | NO | | curdate() | |
| smen_id | int(4) | NO | MUL | NULL | |
+---------+--------+------+-----+-----------+----------------+
4 rows in set (0.002 sec)
MariaDB [niffdb]> SELECT VERSION();
+---------------------------+
| VERSION() |
+---------------------------+
| 10.3.18-MariaDB-0+deb10u1 |
+---------------------------+
1 row in set (0.010 sec)
MariaDB [niffdb]>
While creating a table, you have to use CURRENT_DATE() function as default value. Please see below example I just tested.
CREATE TABLE SALES_DATA (
SALES_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
SALES_GIRL_ID INT UNSIGNED NOT NULL,
SALES_DATE DATE NOT NULL DEFAULT (CURRENT_DATE()),
TOTAL_SALES FLOAT(6, 2),
PRIMARY KEY (SALES_ID),
FOREIGN KEY (SALES_GIRL_ID) REFERENCES SALES_GIRLS(ID)
);

Select SQL Query Not Working With Simple Where clouse

Thee is one table Mysql Table On which simple sql where date = 'Some date' is not working
Have checked logs.
Reload the tables several times & tried.
This is proof that record exists :-
select * from TRN_RP_CONSUMPTION_DAILY limit 1;
+------------+------------------------+----------------------+------------------------+----------------+-------------------+-----------------+-------------------+----------------------+--------------------+----------------------+-------------------+-----------------+---------------+--------------+
| TRCD_DATE | TRCD_SPREAD_START_DATE | TRCD_SPREAD_END_DATE | TRCD_SOURCE_CHENNEL_ID | TRCD_CIRCLE_ID | TRCD_CATALOUGE_ID | TRCD_CONTENT_ID | TRCD_SONG_CODE_ID | TRCD_YT_CP_POLICY_ID | TRCD_YT_CHANNEL_ID | TRCD_PRODUCT_NAME_ID | TRCD_TRXN_TYPE_ID | TRCD_TRXN_COUNT | TRCD_TOP_LINE | TRCD_REVENUE |
+------------+------------------------+----------------------+------------------------+----------------+-------------------+-----------------+-------------------+----------------------+--------------------+----------------------+-------------------+-----------------+---------------+--------------+
| 2018-01-01 | 2018-01-01 | 2018-01-04 | 5 | 1 | 1 | 945723 | 1 | 1 | 1 | 211 | 180 | 1.75 | 0 | 0 |
+------------+------------------------+----------------------+------------------------+----------------+-------------------+-----------------+-------------------+----------------------+--------------------+----------------------+-------------------+-----------------+---------------+--------------+
This is proof that index on date exists :-
select * from TRN_RP_CONSUMPTION_DAILY limit 1;
CREATE TABLE `TRN_RP_CONSUMPTION_DAILY` (
`TRCD_DATE` date NOT NULL DEFAULT '0000-00-00',
`TRCD_SPREAD_START_DATE` date NOT NULL DEFAULT '0000-00-00',
`TRCD_SPREAD_END_DATE` date NOT NULL DEFAULT '0000-00-00',
`TRCD_SOURCE_CHENNEL_ID` smallint(5) unsigned NOT NULL DEFAULT '1',
`TRCD_CIRCLE_ID` smallint(5) unsigned NOT NULL DEFAULT '1',
`TRCD_CATALOUGE_ID` int(10) unsigned NOT NULL DEFAULT '1',
`TRCD_CONTENT_ID` int(10) unsigned NOT NULL DEFAULT '1',
`TRCD_SONG_CODE_ID` int(10) unsigned NOT NULL DEFAULT '1',
`TRCD_YT_CP_POLICY_ID` tinyint(3) unsigned NOT NULL DEFAULT '1',
`TRCD_YT_CHANNEL_ID` tinyint(3) unsigned NOT NULL DEFAULT '1',
`TRCD_PRODUCT_NAME_ID` smallint(5) unsigned NOT NULL DEFAULT '1',
`TRCD_TRXN_TYPE_ID` tinyint(3) unsigned NOT NULL DEFAULT '1',
`TRCD_TRXN_COUNT` double NOT NULL DEFAULT '0',
`TRCD_TOP_LINE` double NOT NULL DEFAULT '0',
`TRCD_REVENUE` double NOT NULL DEFAULT '0',
KEY `IDX_TRCD_DATE` (`TRCD_DATE`),
KEY `IDX_TRCD_SOURCE_CHENNEL_ID` (`TRCD_SOURCE_CHENNEL_ID`),
KEY `IDX_TRCD_CATALOUGE_ID` (`TRCD_CATALOUGE_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
This is the issue, it should give some count but its not coming (See 1st query result above data is present):-
select count(*) from TRN_RP_CONSUMPTION_DAILY where TRCD_DATE='2018-01-01';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
Proof that it is using index :-
explain select count(*) from TRN_RP_CONSUMPTION_DAILY where TRCD_DATE='2018-01-01';
+----+-------------+--------------------------+------+---------------+---------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------------+------+---------------+---------------+---------+-------+------+-------------+
| 1 | SIMPLE | TRN_RP_CONSUMPTION_DAILY | ref | IDX_TRCD_DATE | IDX_TRCD_DATE | 3 | const | 1 | Using index |
+----+-------------+--------------------------+------+---------------+---------------+---------+-------+------+-------------+
Force index also not working :-
select count(*) from TRN_RP_CONSUMPTION_DAILY FORCE INDEX(IDX_TRCD_DATE) where TRCD_DATE='2018-01-01';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
Yes, its huge table :-
select count(*) from TRN_RP_CONSUMPTION_DAILY;
+------------+
| count(*) |
+------------+
| 2006275044 |
+------------+
Table & Index Size :-
103G = TRN_RP_CONSUMPTION_DAILY.MYD
52G = TRN_RP_CONSUMPTION_DAILY.MYI
Surpriseingly this is working, but I can not use alway like this :-
select count(*) from TRN_RP_CONSUMPTION_DAILY where date(TRCD_DATE)='2018-01-01';
+----------+
| count(*) |
+----------+
| 1235523 |
+----------+
I know This is working because index will not come in account when using function on that indexed column.
Which Percona Server :-
Server version: 5.5.60-38.12-log Percona Server (GPL), Release 38.12,
Revision 26ef816
Nothing comes in Error or warning mysql log when query is giving zero count.
Where clouse on other column on which there is index that is working properly.
Can someone help why its not working on that date column?
I want to add some more index on this table but this is not working so I am stopping here.
Move from MyISAM to InnoDB.
Meanwhile, one of these should work. (Go down the list until you get a usable table. Most options are slow because they involve copying the table.)
CHECK TABLE TRN_RP_CONSUMPTION_DAILY; reports an error, do REPAIR TABLE TRN_RP_CONSUMPTION_DAILY;.
OPTIMIZE TABLE TRN_RP_CONSUMPTION_DAILY;
REPAIR TABLE TRN_RP_CONSUMPTION_DAILY USE_FRM;
DROP INDEX ... (for each index), then ADD INDEX ...
copy table over:
CREATE TABLE new LIKE real;
INSERT INTO new SELECT * FROM TRN_RP_CONSUMPTION_DAILY;
RENAME TABLE TRN_RP_CONSUMPTION_DAILY TO old,
new TO TRN_RP_CONSUMPTION_DAILY;
DROP TABLE old;
Restore from backup?
Those are things that are sometimes needed for MyISAM tables; InnoDB is more robust.
Have you tried STR_TO_DATE MySQL function in your where clause?
In your case: TRCD_DATE = STR_TO_DATE('2018-01-01','%Y,%m,%d') (or inverse %m and %d)
For more information: https://www.w3schools.com/sql/func_mysql_str_to_date.asp
I hope this answers to your problem/question.
You have the table as a date, but is it a date or date/time. May be failing on EXACT?
How about changing your where clause to
where TRCD_DATE >='2018-01-01' AND TRCD_DATE < '2018-01-02'
So you are getting anything from 12:00am (morning) all the way up to 11:59:59pm before the next day. Don't try to convert the data column, that will prevent use of an index.

How to set a default value of X for balance Y at user Z

I have the following table;
CREATE TABLE `balance` (
`id` int(10) NOT NULL,
`user_id` int(10) DEFAULT NULL,
`amount` double DEFAULT NULL,
`wallet_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Now the default balance for all users for all "wallets" for when they first sign up is 0, which is what i want.
However how would i change it so that every user_id X receives 1000 amount of of wallet_id 5 when they first sign up, without giving them 1000 of wallet_id 4/6/7/8 (all other wallet balances)?
edit:
*************************** 1. row ***************************
Trigger: update_wallet_trigger
Event: INSERT
Table: users
Statement: BEGIN
UPDATE balance
SET amount = amount + 1000
WHERE user_id = NEW.id AND wallet_id = 1;
END
Timing: AFTER
Created: 2018-04-22 13:48:37.13
sql_mode: NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root#localhost
character_set_client: latin1
collation_connection: latin1_swedish_ci
Database Collation: latin1_swedish_ci
1 row in set (0.01 sec)
My table currently looks like this:
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| user_id | int(10) | YES | | NULL | |
| amount | double | YES | | 0 | |
| wallet_id | int(11) | YES | | NULL | |
+-----------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
With your trigger, it still won't write any new balance/amounts.
Also if a new user is created in USERS, it gets a unique ID, (USER_ID), but that doesn't automatically get ported over to the balance table; the user_id doesn't exist there.
Only User_ID's that already have some sort of balance are listed in the balance table.
As I mentioned in the comments, a trigger would seem to fit your problem well. You may create an after insert trigger on the user table which would fire after a new user record is inserted. This trigger can add a bonus of 1000 to the balance for that user, for wallet_id=5.
DELIMITER //
CREATE TRIGGER update_wallet_trigger
AFTER INSERT
ON users FOR EACH ROW
BEGIN
INSERT INTO balance (user_id, amount, wallet_id)
VALUES
(NEW.id, 1000.0, 5)
ON DUPLICATE KEY UPDATE -- upsert; add 1000 if the record already exists
amount = amount + 1000.0;
END; //
DELIMITER ;
But, you may not even have to do this much. From you application code, you may do the update, right after creating a new user record.
Edit:
I notice that you currently are using a default value of NULL for the amount column in the balance table. Don't do this, because then adding anything to that default value would also result in NULL. Instead, use a default value of zero:
CREATE TABLE balance (
id int(10) NOT NULL,
user_id int(10) NOT NULL,
amount double DEFAULT 0.0,
wallet_id int(11),
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CURRENT_DATE/CURDATE() not working as default DATE value

Pretty straight forward question here, I think this should work but it doesn't. Why doesn't it?
CREATE TABLE INVOICE(
INVOICEDATE DATE NOT NULL DEFAULT CURRENT_DATE
)
It doesn't work because it's not supported
The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column
http://dev.mysql.com/doc/refman/5.5/en/create-table.html
According to this documentation, starting in MySQL 8.0.13, you will be able to specify:
CREATE TABLE INVOICE(
INVOICEDATE DATE DEFAULT (CURRENT_DATE)
)
MySQL 8.0.13 was released to General Availability in October 2018. The release info is located here.
declare your date column as NOT NULL, but without a default. Then add this trigger:
USE `ddb`;
DELIMITER $$
CREATE TRIGGER `default_date` BEFORE INSERT ON `dtable` FOR EACH ROW
if ( isnull(new.query_date) ) then
set new.query_date=curdate();
end if;
$$
delimiter ;
Currently from MySQL 8 you can set the following to a DATE column:
In MySQL Workbench, in the Default field next to the column, write: (curdate())
If you put just curdate() it will fail. You need the extra ( and ) at the beginning and end.
create table the_easy_way(
capture_ts DATETIME DEFAULT CURRENT_TIMESTAMP,
capture_dt DATE AS (DATE(capture_ts))
)
(MySQL 5.7)
I have the current latest version of MySQL: 8.0.20
So my table name is visit, my column name is curdate.
alter table visit modify curdate date not null default (current_date);
This writes the default date value with no timestamp.
----- 2016-07-04 MariaDB 10.2.1 -- Release Note -- -----
Support for DEFAULT with expressions (MDEV-10134).
----- 2018-10-22 8.0.13 General Availability -- -- -----
MySQL now supports use of expressions as default values in data type specifications. This includes the use of expressions as default values for the BLOB, TEXT, GEOMETRY, and JSON data types, which previously could not be assigned default values at all. For details, see Data Type Default Values.
As the other answer correctly notes, you cannot use dynamic functions as a default value. You could use TIMESTAMP with the CURRENT_TIMESTAMP attribute, but this is not always possible, for example if you want to keep both a creation and updated timestamp, and you'd need the only allowed TIMESTAMP column for the second.
In this case, use a trigger instead.
I came to this page with the same question in mind, but it worked for me!, Just thought to update here , may be helpful for someone later!!
MariaDB [niffdb]> desc invoice;
+---------+--------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------+------+-----+---------+----------------+
| inv_id | int(4) | NO | PRI | NULL | auto_increment |
| cust_id | int(4) | NO | MUL | NULL | |
| inv_dt | date | NO | | NULL | |
| smen_id | int(4) | NO | MUL | NULL | |
+---------+--------+------+-----+---------+----------------+
4 rows in set (0.003 sec)
MariaDB [niffdb]> ALTER TABLE invoice MODIFY inv_dt DATE NOT NULL DEFAULT (CURRENT_DATE);
Query OK, 0 rows affected (0.003 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [niffdb]> desc invoice;
+---------+--------+------+-----+-----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------+------+-----+-----------+----------------+
| inv_id | int(4) | NO | PRI | NULL | auto_increment |
| cust_id | int(4) | NO | MUL | NULL | |
| inv_dt | date | NO | | curdate() | |
| smen_id | int(4) | NO | MUL | NULL | |
+---------+--------+------+-----+-----------+----------------+
4 rows in set (0.002 sec)
MariaDB [niffdb]> SELECT VERSION();
+---------------------------+
| VERSION() |
+---------------------------+
| 10.3.18-MariaDB-0+deb10u1 |
+---------------------------+
1 row in set (0.010 sec)
MariaDB [niffdb]>
While creating a table, you have to use CURRENT_DATE() function as default value. Please see below example I just tested.
CREATE TABLE SALES_DATA (
SALES_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
SALES_GIRL_ID INT UNSIGNED NOT NULL,
SALES_DATE DATE NOT NULL DEFAULT (CURRENT_DATE()),
TOTAL_SALES FLOAT(6, 2),
PRIMARY KEY (SALES_ID),
FOREIGN KEY (SALES_GIRL_ID) REFERENCES SALES_GIRLS(ID)
);

Query perfoming full table scan

My query is not using any indexes. Query performing full table scan. What can I do to avoid this?
explain select * from
timed_delivery_messages
where start_time <= '06:39'
and end_time > '06:39'
and mode='Active'
and rotation_weight like '%,45,%'
and substr(day_of_week, 2, 1) = 'T'
limit 1;
Explain Plan
+----+-------------+-------------------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | timed_delivery_messages | ALL | NULL | NULL | NULL | NULL | 22 | Using where |
+----+-------------+-------------------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
Tables:
mysql> show create table timed_delivery_messages\G
*************************** 1. row ***************************
Table: timed_delivery_messages
Create Table: CREATE TABLE `timed_delivery_messages` (
`row_create` datetime DEFAULT NULL,
`row_mod` datetime DEFAULT NULL,
`rule_id` int(11) NOT NULL,
`start_time` time DEFAULT NULL,
`end_time` time DEFAULT NULL,
`day_of_week` varchar(7) DEFAULT NULL,
`rotation_weight` varchar(50) DEFAULT NULL,
`mode` varchar(10) DEFAULT 'active',
`long_message` varchar(256) DEFAULT NULL,
`short_message` varchar(256) DEFAULT NULL,
PRIMARY KEY (`rule_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
You must create one group index with columns
start_time,
end_time,
mode
And also do experiment to include day_of_week into this index. Maybe it will speed up your system