我这里有一个跨国公司给我的SQL培训题,给大家分享一下(2)

本文探讨了如何通过SQL查询实现图片访问统计,并针对不同时间段展示访问次数。同时,提出了性能优化方案及大规模数据生成的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Picture

Id

Name

Path

1

Google Logo

http://www.google.com/logo.png

2

Baidu Logo

http://www.baidu.com/logo.png

3

Yahoo Logo

http://www.yahoo.com/logo.png

4

Live Logo

http://www.live.com/logo.png

 

ViewLog

Id

Picture_Id

View_Date_Time

1

1

2000-01-01 00:00:00

2

1

2000-01-01 00:00:01

3

2

2000-01-01 00:00:01

4

2

2000-01-01 00:00:02

5

3

2000-01-01 00:00:02

6

3

2000-01-01 00:00:03

7

4

2000-01-01 00:00:03

8

4

2000-01-01 00:00:04

 

Question 1: Write a SQL to show the following stats information within the given period - 2000-01-01 00:00:00 to 2000-01-01 00:00:02.

Name

Access_Count

Google Logo

2

Baidu Logo

2

Yahoo Logo

1

Live Logo

0

If I change the period to 2000-01-01 00:00:01 to 2000-01-01 00:00:03, the shown result set should be:

Name

Access_Count

Google Logo

1

Baidu Logo

2

Yahoo Logo

2

Live Logo

1

 

Question 2: Write a SQL to show the following stats information within the given period - 2000-01-01 00:00:00 to 2000-01-01 00:00:02.

Name

Access_Count

Google Logo

2

Baidu Logo

2

Yahoo Logo

1

If I change the period to 2000-01-01 00:00:01 to 2000-01-01 00:00:03, the shown result set should be:

Name

Access_Count

Google Logo

1

Baidu Logo

2

Yahoo Logo

2

Live Logo

1

 

Question 3: Remove all the above data from database. Write a stored procedure to generate 10,000 records in the Picture table, and 26,784,010,000 records in the ViewLog table:

Id

Name

Path

1

Picture #1

http://www.site.com/1.png

2

Picture #2

http://www.site.com/2.png

${n}

Picture #${n}

http://www.site.com/${n}.png

10000

Picture #10000

http://www.site.com/10000.png

 

ViewLog

Id

Picture_Id

View_Date_Time

1

1

2000-01-01 00:00:00

2

2

2000-01-01 00:00:00

${n}

${n}

2000-01-01 00:00:00

10000

10000

2000-01-01 00:00:00

10000+1

1

2000-01-01 00:00:01

10000+2

2

2000-01-01 00:00:01

10000+${n}

${n}

2000-01-01 00:00:01

20000

10000

2000-01-01 00:00:01

${m}*10000+1

1

2000-01-01 00:00:00+${m}

${m}*10000+2

2

2000-01-01 00:00:00+${m}

${m}*10000+${n}

${n}

2000-01-01 00:00:00+${m}

${m}*10000+10000

10000

2000-01-01 00:00:00+${m}

3600*24*31*10000+1

1

2000-01-31 23:59:59

3600*24*31*10000+2

2

2000-01-31 23:59:59

3600*24*31*10000+${n}

${n}

2000-01-31 23:59:59

3600*24*31*10000+10000

10000

2000-01-31 23:59:59

So here should be (3600*24*31+1)*10000 = 26,784,010,000 recodes in the ViewLog table.

After the stored procedure executed, team leads must check the result of his team.

For example:

1.       ‘select count(*) from ViewLog’ should return 26,784,010,000.

2.       ‘select min(Id) from ViewLog’ should return 1.

3.       ‘select max(Id) from ViewLog’ should return 26,784,010,000.

4.       ‘select Picture_Id, count(*) from ViewLog group by Picture_Id’ should return 10,000 records, and all the values in the ‘count(*)’ column is 2,678,401.

You should try other ways to check the data, and make sure the data match the formula in the tables.

 

Question 4: Run your SQL of Q1 and Q2. See what happens. Improve the performance of SQL and table design.

 

Question 5: Run the SQL of Q1 and Q2 with the given period - 2000-01-01 00:00:00 to 2000-01-31 23:59:59, and see what happens. Improve the performance of SQL and table design.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值