来自apache-spark-makes-slow-mysql-queries-10x-faster
For long running (i.e., reporting or BI) queries, it can be much faster as Spark is a massively parallel system. MySQL can only use one CPU core per query, whereas Spark can use all cores on all cluster nodes. In my examples below, MySQL queries are executed inside Spark and run 5-10 times faster (on top of the same MySQL data).
它看起来很棒,但我无法想到查询的实际示例,其中查询可以划分为子查询,并且多核货车使其速度更快 在一个核心上运行它?
最佳答案
假设我们有两个表“客户”和“订单”,每个表都有 1 亿条记录。
现在我们必须在 Customer 和 Order 表中的 customer_id
列上连接这两个表来生成报告,MySQL 几乎不可能做到这一点,因为单个系统必须执行此操作加入大量数据。
在 Spark 集群上,我们可以根据连接列重新分区这些表。现在通过散列 customer_id 来分发两个数据帧的数据。因此,这意味着订单和客户表都具有 Spark 同一工作节点中单个客户的所有数据,并且可以执行本地联接,如下面的代码片段所示。
val customerDf = //
val orderDf = //
val df1 = customerDf.repartition($"customer_id")
val df2 = orderDf.repartition($"customer_id")
val result df1.join(df2).on(df1("customer_id") == df2("customer_id"))
因此,这 1 亿条记录连接现在是在数十或数百个工作节点上并行执行的,而不是像 MySQL 那样在单个节点中完成。
关于java - Apache Spark 让 SQL 查询更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44459346/