简体   繁体   English

如何正确设计 SQL 查询以实现最优行 output

[英]How to properly design of SQL query to achieve most optimal row output

How to properly design a SQL query code to achieve most optimal row output:如何正确设计 SQL 查询代码以实现最优行 output:

select (top 3) C1 from Table where...... (for given input c2 to c9) select (top 3) C1 from Table where...... (对于给定的输入 c2 到 c9)

Suppose there is one table which has 9 (data is percentages values) columns假设有一张表有 9 列(数据是百分比值)

Imagine Table has millions of rows.想象一下 Table 有数百万行。

     C1   C2   C3   C4   C5   C6   C7   C8   C9
     ==========================================
     0.1  0.9  0.4  0.7  0.7  0.3  0.5  0.8  0.9  R1
     0.6  0.7  0.3  0.5  0.8  0.9  0.1  0.9  0.4  R2
     0.1  0.9  0.4  0.7  0.7  0.3  0.5  0.8  0.9  R3
     0.1  0.7  0.3  0.5  0.8  0.9  0.3  0.5  0.8  R4
     0.7  0.7  0.9  0.1  0.9  0.4  0.2  0.2  0.2  R5
     0.7  0.7  0.3  0.5  0.8  0.9  0.4  0.7  0.2  R6

Result would be top 3 C1 values where the sum of absolute difference for each column value is minimal for give input of C2... to C9.结果将是前 3 个 C1 值,其中每列值的绝对差之和对于 C2... 到 C9 的输入最小。

For example if input is C2=0.7, C3=0.3, C4=0.6, C5=0.8 C6=0.9 C7=0.3 C8=0.9 C9=0.2 then例如,如果输入是 C2=0.7, C3=0.3, C4=0.6, C5=0.8 C6=0.9 C7=0.3 C8=0.9 C9=0.2 那么

Top 3 C1 values returned by SQL query should be: SQL 查询返回的前 3 个 C1 值应为:

     Result1 is 0.7 (as result was 0.4 from R6)
     Result2 is 0.6 (as result was 0.5 from R2)
     Result3 is 0.7 (as result was 2.5 from R5) 

Calculations are explained below:计算解释如下:

           C2=0.7    C3=0.3    C4=0.6    C5=0.8    C6=0.9    C7=0.3    C8=0.9    C9=0.2
     R6 =  (0.7-0.7)+(0.3-0.3)+(0.6-0.5)+(0.8-0.8)+(0.9-0.9)+(0.3-0.4)+(0.9-0.7)+(0.2-0.2)
     absolute  0    +   0     +   0.1   +   0     +   0     +   0.1     +   0.2 +   0
     result = 0.4


     R5 =  (0.7-0.7)+(0.3-0.9)+(0.6-0.1)+(0.8-0.9)+(0.9-0.4)+(0.3-0.2)+(0.9-0.2)+(0.2-0.2)
     absolute  0.0  +   0.6   +   0.5   +   0.1   +   0.5   +   0.1   +   0.7   +   0.0
     result = 2.5

     R2 =  (0.7-0.7)+(0.3-0.3)+(0.6-0.5)+(0.8-0.8)+(0.9-0.9)+(0.3-0.1)+(0.9-0.9)+(0.2-0.4)
     absolute  0.0  +   0.0   +   0.1   +   0.0   +   0.0   +   0.2   +   0.0   +   0.2
     result = 0.5

This should give you C1 values ordered ascending by your formula:这应该为您提供按公式升序排列的 C1 值:

SELECT C1
FROM PERCENTAGES
ORDER BY ABS(C2 - 0.7) + ABS(C3 - 0.3) + ABS(C4 - 0.6) + ABS(C5 - 0.8) + 
         ABS(C6 - 0.9) + ABS(C7 - 0.3) + ABS(C8 - 0.9) + ABS(C9 - 0.2);

Tested with Oracle... but it's plain SQL that should work anywhere.用 Oracle 测试......但它是普通的 SQL 应该可以在任何地方工作。 To get the top 3 values, you'll have to apply a DB-specific clause.要获得前 3 个值,您必须应用特定于数据库的子句。

For MySQL it should be easy.对于 MySQL 应该很容易。 Just add a LIMIT 3 to the query.只需在查询中添加一个LIMIT 3即可。

I recommend to replace the constant input with variables and use a prepared statement to actually run the query.我建议用变量替换常量输入,并使用准备好的语句来实际运行查询。 But that's a topic for another question.但这是另一个问题的话题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM