首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更快的BigQuery数据在闪亮

更快的BigQuery数据在闪亮
EN

Stack Overflow用户
提问于 2022-03-24 17:51:54
回答 1查看 97关注 0票数 0

我不喜欢访问完整的公共BigQuery数据集(在我的例子中是geo_us_boundaries )。但是我想通过state使用查询glue::glue_sql("SELECT * FROM states WHERE state = {x}", x = input$selectedvariable1, .con=bq_con)来选择数据。我试着做:

代码语言:javascript
复制
library(dplyr)
library(ggplot2)
library(bigrquery)
library(DBI)
library(sf)
library(glue)

# Open a public BigQuery dataset eg. "geo_us_boundaries"
bq_con <- dbConnect(
  bigrquery::bigquery(),
  project = "bigquery-public-data",
  dataset = "geo_us_boundaries",
  billing = "my-project"
)
bigrquery::dbListTables(bq_con) # List all the tables in BigQuery data set


# Take the table
dataset <- dplyr::tbl(bq_con, 
                      "states") # connects to a table


# Enumerate the states
dataset_vars <- dataset %>% dplyr::distinct(geo_id, state, state_name)%>% 
  collect() 
str(dataset_vars)


# Create the shiny dash
ui <- fluidPage(
  titlePanel(title="States Dashboard"),  
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "selectedvariable0",
                  label = "Geo ID", 
                  choices = c(unique(dataset_vars$geo_id)),selected = TRUE ), 
      selectInput(inputId = "selectedvariable1",
                  label = "State", 
                  choices = c(unique(dataset_vars$state)),selected = TRUE ), 
      selectInput(inputId = "selectedvariable2",
                  label = "State name", 
                  choices = c(unique(dataset_vars$state_name)),selected = TRUE )
    ),
    mainPanel(
      fluidRow(
        splitLayout(plotOutput("myplot")))
      
    )
  )
)
server <- function(input, output){
  
  # # Selection of variables for plots constructions
  
  sqlInput <- reactive({
    glue::glue_sql("SELECT * FROM states WHERE state = {x}", x = input$selectedvariable1, .con=bq_con)
  })
  stands_sel <- function() dbGetQuery(bq_con, as.character(sqlInput()), stringsAsFactors = T)
  
  print(sqlInput)

  observe({
    
    output$myplot <- renderPlot({
      
      #Create the plot 
      stands_sel_sf <- st_as_sf(stands_sel(), wkt = "state_geom", crs = 4326) 
      ggplot() + geom_sf(data=stands_sel_sf) }) 
  }) #end of observe function.
}
shinyApp(ui, server)
#

但是state选择的这种类型的数据访问非常缓慢,在我的现实世界中,我有很大的几何图形,我花了几分钟从by下载和完成了地块。有什么办法来提高这一行动的速度吗?

能帮个忙吗?

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-26 04:22:49

你确定花在下载数据,而不是渲染数据的时间吗?查询需要几秒钟,即使是几何文本大小(德州)最大的州也只有1.3MB。这不应该花那么多时间下载。

无论如何,一个典型的解决方案是简化状态几何:

代码语言:javascript
复制
SELECT * EXCEPT(state_geom), ST_Simplify(state_geom, 1000) AS state_geom 
FROM states WHERE state = {x}

这使得最重的州(现在的阿拉斯加)小于40 to的大小,应该更快地下载和绘制。

请注意,它每次在查询中执行ST_Simplify计算,这有点昂贵。您可能只想做一次--使用简化的状态几何创建表并从中读取--这也使得查询本身更便宜、更快,因为查询读取的数据较少,而且不必计算简化的几何图形。

代码语言:javascript
复制
CREATE TABLE tmp.states AS
SELECT * EXCEPT(state_geom), ST_Simplify(state_geom, 1000) AS state_geom
FROM  `bigquery-public-data`.geo_us_boundaries.states;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71607246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档