Business Practice Record 5: Exceptions and Thoughts Caused by MySQL Field Aliases

introduction

In daily combat, database query is a key link in the process of data analysis and decision-making. However, due to conflicts between existing fields and field aliases, we may encounter unexpected errors and confusion. Therefore, in order to ensure the accuracy and reliability of query results, we must carefully handle the naming of fields and aliases in query statements to avoid conflicts.
This article explores unexpected errors caused by field conflicts through a real-world case, and explores the advantages and disadvantages of field aliasing.

case analysis

I received a piece of feedback from the business team today: 600 to 700% of the xxx data appeared. Theoretically, this value should be less than 100%, how did it become 600+%!
View the relevant MySQL code, the logic is as follows:

# 注:由于某些原因分两步进行统计,这是抽象后的结果,实际表单数据并非如此,而且比这个复杂。
with agg_actions as(
    select date(action_one_time) "动作1日期"
        ,date(action_two_time)   "动作2日期"
        ,production_name         "商品"
        ,count(action_one_time)  "动作1人数"
        ,count(action_two_time)  "动作2人数"
    from actions
    group by `动作1日期`,`动作2日期`,`商品`
)
select 
	 -- 按周统计
	 date_sub(动作1日期,interval weekday(动作1日期) day) as "动作1日期"
    ,sum(动作2人数)/sum(动作1人数) as "动作转化率" 
from agg_actions
group by `动作1日期`

At first glance, there seems to be no problem with the code, but when debugging, it is easy to post. It is a bug in the field alias, but it is not clear why 600+% appears. Blind guessing is to make statistics according to each day, and then Sum by week. In short, there is an inexplicable error.
Solution :
The solution is actually very simple, just change the name of line 12 of the SQL, and then keep the name of line 15 consistent with line 12.

Pros and cons of field aliasing

As a solution, field aliases have obvious advantages and can help us improve efficiency.
Here are 4 advantages:

  • Improve the readability and understandability of the code: such as giving fields more descriptive names;
  • Simplify complex queries: such as simplifying field references in complex queries, such as the aliases of the above codes, mostly simplify complex queries into an alias for later reference;
  • Disambiguation: Disambiguation is the solution to the above example
  • Integration with applications and stability maintenance: alias fields can improve the integration capabilities of databases and applications, and reduce application modification due to changes in database schemas. This is generally rare, and the database schema will not be changed arbitrarily if nothing happens. Only The database mode will only be activated in major iterations, so the overall situation will be relatively stable.

Of course, things always have two sides, there are good and bad, and field aliases also have some potential disadvantages:

  • Field ambiguity: As mentioned in the above example, statistical errors are caused by improper use;
  • Difficulty in maintenance: When the code is long and complex, and there are too many aliases, maintenance will be quite difficult, and the readability of the code will become poor;
  • Additional processing overhead: Aliasing fields requires additional processing steps and computing resources. Aliasing fields may have some impact on performance when dealing with large amounts of data or complex queries.

So, when using field aliases, a few tips:

  • Naming consistency: ensure that the naming conventions and naming conventions of alias fields are consistent with database design and business requirements;
  • Avoid overuse: use field aliases only when necessary, and avoid complicating query statements due to excessive use of aliases;
  • Testing and verification: After the alias name field, conduct sufficient testing and verification to ensure the accuracy of the query results and the acceptability of the performance.
  • Documentation and comments: Use appropriate documentation and comments in the query statement to explain the purpose and meaning of the alias field to facilitate future maintenance and understanding.

in conclusion

Field aliases are an effective way to solve the problem of field and table alias conflicts. It improves query readability, simplifies complex queries, and helps maintain application stability. However, it is critical to properly weigh the pros and cons of field aliasing. By following best practices and being aware of potential downsides, we can take full advantage of the benefits of field aliasing while mitigating potential risks and difficulties.

Guess you like

Origin blog.csdn.net/qq_45476428/article/details/130895510