dongzipu7517 2017-11-27 19:18
浏览 43

在数据库(SQL)和编程领域执行操作和逻辑的好处(PHP,C#...)

What are the benefits of performing some functions or operations in the database compared to doing them in the some other programming language?

For example, let's assume there is a mysql database school with table students. Then the students table has the following attributes (id, name, age, dob...): will it be better to do like the age calculation in the database by subtracting the dob from the current date and then storing it in age column for all students? Or is it better to calculate it in a programming language and then store the age result into the database?

The goal here is to let each student's age update automatically each year. Also to make sure the application performance is top notch(which includes speed).

So which method is better?

  • 写回答

1条回答 默认 最新

  • douke8473 2017-11-28 06:54
    关注

    The best idea is to store only the DATE_OF_BIRTH in the database. When the application needs to know a student's age it can calculate it using the current date.

    As for whether it's better to do the calculations in the database or the application, usually you should so in SQL what can be done in SQL. Given that we have to query the data anyway, no application code is going to be speedier than

    select student_id
            , date_of birth
            , floor(months_between(sysdate, date_of_birth)/12) as age_in_years
    from students
    

    This solution uses Oracle RDBMS functions; the precise syntax will be different for SQL Server or MySQL.


    "am looking to make sure the application performs well and fast"

    The Hadoopers often talk about bringing the computation to the data. Well that's not a new insight, that's what SQL does, and has been doing for decades.

    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单