mysql multiple group by

时间:2017-08-30 20:18:57

标签: mysql group-by

Good time! Tell me how to optimize this query and, if possible, unite it into one!

flexbox

2 个答案:

答案 0 :(得分:0)

实际上,每个查询都有不同的分组和排序依据,所以最好的办法就是使用UNION

$query = <<<QRY
SELECT external_source_id as ID
  FROM smf_tds_unique_statistic
  WHERE $dateRange
  GROUP BY external_source_id
  ORDER BY external_source_id ASC
UNION
SELECT sources_id ID
  FROM smf_tds_unique_statistic
  WHERE $dateRange
  GROUP BY sources_id 
  ORDER BY sources_id ASC
UNION
SELECT ip_country ID
  FROM smf_tds_unique_statistic 
  WHERE $dateRange
  GROUP BY ip_country
  ORDER BY ip_country ASC
UNION
SELECT cidr ID
  FROM smf_tds_unique_statistic
  WHERE $dateRange
  GROUP BY cidr 
  ORDER BY cidr ASC
QRY;

你应该明白这个想法。另一种方法是使用日期过滤器一次并创建一个INDEXED TEMP表并在您的UNION中使用THAT。取决于$dateRange过滤器的记录数和费用

答案 1 :(得分:-1)

It appears your queries only need to get lists of distinct values in four columns.

Here's a way you can do this with one query:

> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.7.2     purrr_0.2.3     readr_1.1.1     tidyr_0.7.0     tibble_1.3.4    ggplot2_2.2.1   tidyverse_1.1.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.12     cellranger_1.1.0 compiler_3.4.1   plyr_1.8.4       bindr_0.1        forcats_0.2.0    tools_3.4.1      lubridate_1.6.0 
 [9] jsonlite_1.5     nlme_3.1-131     gtable_0.2.0     lattice_0.20-35  pkgconfig_2.0.1  rlang_0.1.2      psych_1.7.5      parallel_3.4.1  
[17] haven_1.1.0      bindrcpp_0.2     xml2_1.1.1       stringr_1.2.0    httr_1.3.1       hms_0.3          tidyselect_0.2.0 grid_3.4.1      
[25] glue_1.1.1       R6_2.2.2         readxl_1.0.0     foreign_0.8-69   modelr_0.1.1     reshape2_1.4.2   magrittr_1.5     scales_0.5.0    
[33] rvest_0.3.2      assertthat_0.2.0 mnormt_1.5-5     colorspace_1.3-2 stringi_1.1.5    lazyeval_0.2.0   munsell_0.4.3    broom_0.4.2   

The values will be returned in a single row, and each column will contain a string which is a comma-separated list of distinct values from each respective column in the database. You'll have to use PHP to explode() each of these strings into an array.

The only way you might optimize this query is to create an index for your R version 3.4.1 (2017-06-30) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Sierra 10.12.6 Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] dplyr_0.7.2 tidyr_0.7.0 loaded via a namespace (and not attached): [1] tidyselect_0.1.1 compiler_3.4.1 assertthat_0.2.0 R6_2.2.2 magrittr_1.5 tools_3.4.1 bindrcpp_0.2 glue_1.1.1 tibble_1.3.4 [10] yaml_2.1.14 Rcpp_0.12.12 pkgconfig_2.0.1 rlang_0.1.2 bindr_0.1 purrr_0.2.3 condition to use. But you haven't included the value of tidyverse in your question, so I'll leave it to you to figure that out.