MySQL:从小到大的表

时间:2016-10-18 11:58:05

标签: mysql performance join inner-join

我有以下表格:

CREATE TABLE smalltable (
    smalltable_id VARCHAR(64) NOT NULL,
    bigtable_id VARCHAR(64),
    ...
    PRIMARY KEY (smalltable_id)
) ENGINE=InnoDB;

CREATE TABLE bigtable (
    bigtable_id VARCHAR(64) NOT NULL,
    count BIGINT,
    PRIMARY KEY (bigtable_id)
) ENGINE=InnoDB;

smalltable大约有8000行,bigtable大约有4000万行。我想从smalltable smalltable.bigtable_idbigtable SELECT * FROM smalltable INNER JOIN bigtable ON smalltable.bigtable_id = bigtable.bigtable_id; 中检索这些行。以下查询花了将近10个小时才完成:

EXPLAIN

以下是*************************** 1. row *************************** id: 1 select_type: SIMPLE table: smalltable partitions: NULL type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 8610 filtered: 100.00 Extra: NULL *************************** 2. row *************************** id: 1 select_type: SIMPLE table: bigtable partitions: NULL type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 38818260 filtered: 100.00 Extra: Using where; Using join buffer (Block Nested Loop) 的输出:

smalltable

我没有专家解释这一点,但看起来MySQL正在对两个表进行顺序扫描。如果我编写一个Python脚本来迭代SELECT中的所有行,并对bigtablesmalltable的每一行执行foreach($request->category as $category) { $catphoto = new Category_Photo(['photo_id' => $photo->id, 'category_id' => $category]); $photo->categories()->save($catphoto); } foreach($request->size as $size) { $photosize = new Photo_Size(['photo_id' => $photo->id, 'size_id' => $size]); $photo->sizes()->save($photosize); } 查询,那么整个事情将在25秒内完成。我希望通过单个SQL查询获得相同的性能。

2 个答案:

答案 0 :(得分:0)

检查优化器路径,哪个表是主要来源,就好像它很慢,bigtable用作主要来源。试试这个:

FATAL:   JSF1073: javax.el.PropertyNotFoundException caught during processing of RENDER_RESPONSE 6 : UIComponent-ClientId=, Message=/index.xhtml @150,74 value="#{restaurants.seats}": The class 'c.c.c.c.c.Restaurants' does not have the property 'seats'.
FATAL:   /index.xhtml @150,74 value="#{restaurants.seats}": The class 'c.c.c.c.c.Restaurants' does not have the property 'seats'.
javax.el.PropertyNotFoundException: /index.xhtml @150,74 value="#{restaurants.seats}": The class 'c.c.c.c.c.Restaurants' does not have the property 'seats'.
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:174)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at org.primefaces.component.column.Column.renderChildren(Column.java:304)
    at org.primefaces.component.datatable.DataTableRenderer.encodeCell(DataTableRenderer.java:963)
    at org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:919)
    at org.primefaces.component.datatable.DataTableRenderer.encodeRows(DataTableRenderer.java:830)
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:777)
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:740)
    at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:264)
    at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:226)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.el.PropertyNotFoundException: The class 'c.c.c.c.c.Restaurants' does not have the property 'seats'.
    at javax.el.BeanELResolver.getBeanProperty(BeanELResolver.java:731)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:351)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:140)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:204)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    ... 56 more

使用STRAIGHT_JOIN将告诉mysql遵循查询中表的顺序。

答案 1 :(得分:0)

如果您只需要smalltable中的信息(这是您的描述所暗示的),

SELECT *
    FROM smalltable AS s
    WHERE EXISTS (
        SELECT *
            FROM bigtable
            WHERE bigtable_id = s.bigtable_id );