CREATE TABLE IF NOT EXISTS `posts` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`user` varchar(9) NOT NULL,
`post` text NOT NULL,
`date` varchar(14) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MyISAM DEFAULT CHARSET=utf8;
table created
INSERT INTO `posts` ( 'user', 'post', 'date') VALUES
('she', 'dolphin', '???');
('me', 'chandra'), '???');
Instead of ??? I need current datetime in the following format:day.month. hour:min
For example for today and now: 03.01. 00:34
That a bad idea, because you are storing the data
in a format that can't be ordered
in a format that mySQL can't optimize in any meaningful way
without the year of the date
Use a proper mySQL DATETIME field instead, and format the output using DATE_FORMAT when you make the query:
SELECT DATE_FORMAT(`date`, "%d.%m. %H:%i") FROM `posts`;
Related
I have searched quite a bit and can't seem to find the answer to this. I am fairly new to MySQL and this is something I had previously written as a php script to update the individual tables, but would rather it update automatically on the database when the table changes.
When I update a table via a google script I then require about 6 different tables to be updated from different rows in the main table, by date, and each table requires data from a different column.
I know this is not the correct way to do it but I need the trigger to effectively do the following;
CREATE TRIGGER dash_1
ON global_summary AFTER UPDATE
UPDATE widget_dash_2 (date, metric)
WHERE inc_id=100 SELECT date, volume
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 1))
UPDATE widget_dash_2 (date, metric)
WHERE inc_id=101 SELECT date, volume
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 8))
This would be for one of the columns "volume" from table "global_summary", I then need to add up to another 5 updates such as;
UPDATE widget_dash_3 (date, metric)
WHERE inc_id=100 SELECT date, score
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 1))
UPDATE widget_dash_3 (date, metric)
WHERE inc_id=101 SELECT date, score
FROM global_summary
WHERE (`date` = SUBDATE(CURDATE(), 8))
Where "score" is another column in the table "global_summary".
Any guidance would be much appreciated. I have been looking for some guidance on this for days now and it has just made me more confused.
I'm assuming I need to do something with arrays and initially getting all data from the main table, but how can I then carve that up?
#sticky bit,
Unfortunately I have to put this data into these small tables, believe me I would much rather just pull it from global_summary directly.
CREATE TABLE `global_summary` (
`id` int(11) DEFAULT NULL,
`date` date DEFAULT NULL,
`volume` int(10) DEFAULT NULL,
`volume_problem` int(10) DEFAULT NULL,
`removed` int(10) DEFAULT NULL,
`score` int(10) DEFAULT NULL,
`error` int(10) DEFAULT NULL,
`total` int(10) DEFAULT NULL,
`previous` int(10) DEFAULT NULL,
`entryID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
All the widget_dash tables;
CREATE TABLE `widget_dash_2` (
`inc_id` int(11) NOT NULL,
`date` date DEFAULT NULL,
`metric` int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Thanks for your feedback so far, and yes I probably was confusing UPDATE with INSERT.
I have the following table in MySQL:
CREATE TABLE `history` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`code` CHAR(32) NOT NULL,
`value` FLOAT NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `timestamp_code` (`timestamp`, `code`),
INDEX `code` (`code`),
INDEX `timestamp` (`timestamp`)
) COLLATE='utf8_general_ci' ENGINE=InnoDB;
I would like to know what is the best practice in order to access the last available value before a certain date for a certain set of codes the most efficiently?
So far I came up with the following query:
SELECT h.* FROM history h
JOIN (
SELECT code, MAX(timestamp) as 'last_ts'
FROM history WHERE
timestamp < '2015-09-04 13:50:00' AND
code IN ('119813249', '12087792', '12087797',
'127012151', '131014335', '131014378',
'132757371', '15016059', '15016062',
'150250238', '153462747', '155802712',
'156974389', '162277696', '166330444',
'166483001', '167220356', '167264923',
'167867931', '172283682', '177539478',
'177583937', '177648754', '177649011',
'187532416', '189230667', '70273253',
'70342790', '79342386', '82460282',
'98693280', '98693380')
GROUP BY code) last_price
ON last_price.last_ts = h.timestamp
AND last_price.code = h.code
The query above works, but becomes slow as the number of entries in the table grows (100'000'000 rows).
You can download sample data to populate the table.
Create an index by code, timestamp - rather than timestamp, code. This would let mysql sort out codes before looking for the max timestamp per code - and should be much faster. Use explain for verifying that the index is used.
And if you create that index - you should not have to modify your query.
I have two tables with the following schema,
CREATE TABLE `open_log` (
`delivery_id` varchar(30) DEFAULT NULL,
`email_id` varchar(50) DEFAULT NULL,
`email_activity` varchar(30) DEFAULT NULL,
`click_url` text,
`email_code` varchar(30) DEFAULT NULL,
`on_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `sent_log` (
`email_id` varchar(50) DEFAULT NULL,
`delivery_id` varchar(50) DEFAULT NULL,
`email_code` varchar(50) DEFAULT NULL,
`delivery_status` varchar(50) DEFAULT NULL,
`tries` int(11) DEFAULT NULL,
`creation_ts` varchar(50) DEFAULT NULL,
`creation_dt` varchar(50) DEFAULT NULL,
`on_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The email_id and delivery_id columns in both tables make up a unique key.
The open_log table have 2.5 million records where as sent_log table has 0.25 million records.
I want to filter out the records from open log table based on the unique key (email_id and delivery_id).
I'm writing the following query.
SELECT * FROM open_log
WHERE CONCAT(email_id,'^',delivery_id)
IN (
SELECT DISTINCT CONCAT(email_id,'^',delivery_id) FROM sent_log
)
The problem is the query is taking too much time to execute. I've waited for an hour for the query completion but didn't succeed.
Kindly, suggest what I can do to make it fast since, I have the big data size in the tables.
Thanks,
Faisal Nasir
First, rewrite your query using exists:
SELECT *
FROM open_log ol
WHERE EXISTS (SELECT 1
FROM send_log sl
WHERE sl.email_id = ol.email_id and sl.delivery_id = ol.delivery_id
);
Then, add an index so this query will run faster:
create index idx_sendlog_emailid_deliveryid on send_log(email_id, delivery_id);
Your query is slow for a variety of reasons:
The use of string concatenation makes it impossible for MySQL to use an index.
The select distinct in the subquery is unnecessary.
Exists can be faster than in.
If this request is often on, you can greatly increase it by create bigint id column, enven if it not unique.
For example you can put trigger and create column like this
alter table sent_log for_get bigint;
After that create trigger/ update it to put hash into that bigint
for_get=CONV(substr(md5(concat(email_id, delivery_id)),1,10),16,10)
If you have such column in both table and index on it, query will be like
SELECT *
FROM open_log ol
left join send_log sl on sl.for_get=ol.for_get
WHERE sl.email_id is not null and sl.email_id = ol.email_id and sl.delivery_id = ol.delivery_id;
That query will be fast.
I am facing some issue in query execution here is my case :
I have two tables log with 2 lakh records and logrecords with 6 lakh records
Where single log record in log table can have multiple log messages in logrecords table my database schema is as below
log Table
CREATE TABLE `log` (
`logid` varchar(50) NOT NULL DEFAULT '',
`creationtime` bigint(20) DEFAULT NULL,
`serviceInitiatorID` varchar(50) DEFAULT NULL,
PRIMARY KEY (`logid`),
KEY `idx_creationtime_wsc_log` (`creationtime`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
And logrecords Table
CREATE TABLE `logrecords` (
`logrecordid` varchar(50) NOT NULL DEFAULT '',
`timestamp` bigint(20) DEFAULT NULL,
`message` varchar(8000) DEFAULT NULL,
`loglevel` int(11) DEFAULT NULL,
`logid` varchar(50) DEFAULT NULL,
`indexcolumn` int(11) DEFAULT NULL,
PRIMARY KEY (`logrecordid`),
KEY `indx_logrecordid_message_logid` (`logrecordid`,`message`(767),`logid`),
KEY `logid` (`logid`),
KEY `indx_message` (`message`(767))
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Query created by hibernate is like
select this_.logid as logid4_1_, this_.loglevel as loglevel4_1_, this_.creationtime as creation3_4_1_,this_.serviceInitiatorID as service17_4_1_, this_.logtype as logtype4_1_,logrecord1_.logrecordid as logrecor1_3_0_, logrecord1_.timestamp as timestamp3_0_, logrecord1_.message as message3_0_, logrecord1_.loglevel as loglevel3_0_, logrecord1_.logid as logid3_0_, logrecord1_.indexcolumn as indexcol6_3_0_ from log this_ inner join wsc_logrecords logrecord1_ on this_.logid=logrecord1_.logid where (1=1) and (1=1) and logrecord1_.message like 'SecondMessage' order by this_.creationtime desc limit 25
Which taking around 7313ms to execute
Query Explain is like
But when I execute below query it is taking around 15 min to execute
select count(*) as y0_ from log this_ inner join logrecords logrecord1_ on this_.logid=logrecord1_.logid where (1=1) and (1=1) and lower(logrecord1_.message) like 'SecondMessage' order by this_.creationtime desc limit 25
For above query explain is like
and I am using MySQl database. I think there is some issue in indexing or some other which I am not able to identify
Any solution will be appreciated.
When you use lower(logrecord1_.message) like 'SecondMessage' instead of plain logrecord1_.message like 'SecondMessage' the DB engine will stop using the index on logrecord1_.message.
You can overcome this by creating a function based index that has lower(logrecord1_.message) in place of logrecord1_.message.
CREATE TABLE `articles_entities` (
`id` CHAR(36) NOT NULL,
....
`created` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `created` (`created`)
) ENGINE=MYISAM DEFAULT CHARSET=utf8;
I am trying to create a query that gives me the count of records by day.. e.g
Day 1: 23
Day 2: 343
etc...
Please note the output is not the exact format I want, just a display of what data I want.
While writing the question I realised just how easy this is....
SELECT COUNT(*), DATE_FORMAT(created, '%Y%m%d') AS testgroup FROM articles_entities GROUP BY testgroup;