带有时间戳的SQL测试 [英] SQL Test with timestamp

查看:87
本文介绍了带有时间戳的SQL测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我必须回答以下问题才能接受新工作的面试。你能证实我的答案是否正确吗?我不确定因为时间戳数据类型和变量'login_time'的CURRENT_TIMESTAMP。非常感谢。


使用下面的表定义并假设这样的表存在7.5亿条记录,编写一个有效的SQL
查询(或查询),返回登录总数和2015年1月1日至2015年2月15日期间登录的唯一配置文件的数量,包括在内。  解释你的逻辑。


创建表`LoginLog`(


 `id` int (11)unsigned NOT NULL AUTO_INCREMENT,


 `login_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,


 `profile_id` int(11)DEFAULT'-1',


  `mailer_id` int(10)unsigned DEFAULT'0',


  `entry_point` enum('UNKNOWN','MAIN_SITE','REGISTRATION','MAILER','MOBILE')NOT NULL DEFAULT'UNKNOWN',


  PRIMARY KEY(`id`),


  KEY`profile_id_idx`(`profile_id`))


  ENGINE = MyISAM AUTO_INCREMENT = 459520272 DEFAULT CHARSET = utf8


);




我的回答是:


1-  2015年1月1日至2015年2月15日期间登录的唯一配置文件总数的脚本,包含



选择计数(distinct profile_id)


来自LoginLog


其中login_time是  > =&NBSP; '2015-01-01 00 00 01'和login_time是  < ='2015-02-15 23 59 59';



< p style ="margin-bottom:0cm; margin-bottom:.0001pt"> 2-Script返回2015年1月1日到2015年2月15日之间的登录总数,包括在内。


< p style ="margin-bottom:0cm; margin-bottom:.0001pt">


- 假设:1 line = 1 login -



选择计数(Id)


来自LoginLog


其中login_time是  > =&NBSP; '2015-01-01 00 00 01'和login_time是  < ='2015-02-15 23 59 59';


















解决方案

有一件事是你错过了在  2015-01-01 00 00 00.000和  2015-01-01
00 00 00.997。以及 
2015-02-15 23 59 59.003和  2015-02-15
23 59 59.997。


不,等等。我们无法回答你的问题,因为它显然不是SQL Server问题。数据类型显然是
时间戳,而timestamp数据类型与date和date无关因此,我们需要知道他们想到的DBMS以及该DBMS中时间戳数据类型的详细信息。但是,我们更喜欢以下类型的搜索
作为日期时间范围:


其中login_time是 > = '2015-01-01 00:00:00'和login_time是 <'2015-02-16 00:00:00';


上面的逻辑也适用于其他DBMS,它是SARGable。


Hello, I have to answer the following question to have a interview for a new job. Could you verify if my answer is correct ? I am not sure because of the timestamp data type and the CURRENT_TIMESTAMP for the variable 'login_time'. Thank you very much.

Using the table definition below and assuming such table exists with 750 million records, write an efficient SQL query (or queries) that returns the total number of logins and the number of unique profiles that logged in between January 1, 2015 and February 15, 2015, inclusively.  Explain your logic.

CREATE TABLE `LoginLog` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `login_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  `profile_id` int(11) DEFAULT '-1',

  `mailer_id` int(10) unsigned DEFAULT '0',

  `entry_point` enum('UNKNOWN','MAIN_SITE','REGISTRATION','MAILER','MOBILE') NOT NULL DEFAULT 'UNKNOWN',

  PRIMARY KEY (`id`),

  KEY `profile_id_idx` (`profile_id`))

  ENGINE=MyISAM AUTO_INCREMENT=459520272 DEFAULT CHARSET=utf8

);

My answer are :

1-  Script for total number of unique profiles that logged in between January 1, 2015 and February 15, 2015, inclusively

Select count (distinct profile_id)

From LoginLog

Where login_time is  >=  ‘2015-01-01 00 00 01’ and login_time is  <= ‘2015-02-15 23 59 59’;

2-Script for returns the total number of logins between January 1, 2015 and February 15, 2015, inclusively.

-- Hypothesis : 1 line = 1 login--

Select count (Id)

From LoginLog

Where login_time is  >=  ‘2015-01-01 00 00 01’ and login_time is  <= ‘2015-02-15 23 59 59’;






解决方案

One thing is that you are missing logins done between 2015-01-01 00 00 00.000 and 2015-01-01 00 00 00.997. And also between 2015-02-15 23 59 59.003 and 2015-02-15 23 59 59.997.

No, wait. We cannot answer your question since it apparently isn't an SQL Server question. The data type is apparently timestamp, and the timestamp datatype has nothing to do with date and time in SQL Server. So, we would need to know what DBMS they have in mind and also the details for the timestamp data type in that DBMS. In genera, though, we prefer below type of searches for date time ranges:

Where login_time is  >=  ‘2015-01-01 00:00:00’ and login_time is  < ‘2015-02-16 00:00:00’;

Above logic should also work for other DBMSs and it is SARGable.


这篇关于带有时间戳的SQL测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆