如何重写此MySQL查询以使用更少的内存?

时间:2014-03-17 17:51:34

标签: mysql

我正在练习使用示例mysql数据库编写mysql查询,该数据库在员工和工资中有500,000个条目(参见下表)。我想要一份经理人名单,这些经理人不是财务人员,而是比至少一个财务人员赚更多的钱。 (DEPT_NO = d002

查询:

SELECT salaries.salary, employees.first_name, employees.last_name 
FROM salaries, employees, dept_manager, dept_emp
WHERE employees.emp_no=dept_manager.emp_no
AND dept_manager.dept_no <> "d002"
AND salaries.emp_no = dept_manager.emp_no
AND salaries.salary > ANY 
    (SELECT salary FROM salaries, dept_emp 
        WHERE dept_emp.dept_no="d002" 
        AND salaries.emp_no = dept_emp.emp_no); 

此查询崩溃了sql server,因为它在内存耗尽之前才能完成。这是在MacBook 10.6 2.2GB核心二重奏和2Gb RAM上。虽然这可能要求很多但我觉得应该有一种方法来做这个有限的计算能力。我已经完成了其他查询而没有崩溃(它们很慢但仍然存在)

好奇的错误信息:

mysql(611) malloc: *** mmap(size=3870720) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
mysql: Out of memory (Needed 3869736 bytes)
ERROR 2008 (HY000): MySQL client ran out of memory

表:

CREATE TABLE employees (
    emp_no      INT             NOT NULL,
    birth_date  DATE            NOT NULL,
    first_name  VARCHAR(14)     NOT NULL,
    last_name   VARCHAR(16)     NOT NULL,
    gender      ENUM ('M','F')  NOT NULL,    
    hire_date   DATE            NOT NULL,
    PRIMARY KEY (emp_no)
);

CREATE TABLE departments (
    dept_no     CHAR(4)         NOT NULL,
    dept_name   VARCHAR(40)     NOT NULL,
    PRIMARY KEY (dept_no),
    UNIQUE  KEY (dept_name)
);

CREATE TABLE dept_manager (
   dept_no      CHAR(4)         NOT NULL,
   emp_no       INT             NOT NULL,
   from_date    DATE            NOT NULL,
   to_date      DATE            NOT NULL,
   KEY         (emp_no),
   KEY         (dept_no),
   FOREIGN KEY (emp_no)  REFERENCES employees (emp_no)    ON DELETE CASCADE,
   FOREIGN KEY (dept_no) REFERENCES departments (dept_no) ON DELETE CASCADE,
   PRIMARY KEY (emp_no,dept_no)
); 

CREATE TABLE dept_emp (
    emp_no      INT             NOT NULL,
    dept_no     CHAR(4)         NOT NULL,
    from_date   DATE            NOT NULL,
    to_date     DATE            NOT NULL,
    KEY         (emp_no),
    KEY         (dept_no),
    FOREIGN KEY (emp_no)  REFERENCES employees   (emp_no)  ON DELETE CASCADE,
    FOREIGN KEY (dept_no) REFERENCES departments (dept_no) ON DELETE CASCADE,
    PRIMARY KEY (emp_no,dept_no)
);

CREATE TABLE titles (
    emp_no      INT             NOT NULL,
    title       VARCHAR(50)     NOT NULL,
    from_date   DATE            NOT NULL,
    to_date     DATE,
    KEY         (emp_no),
    FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
    PRIMARY KEY (emp_no,title, from_date)
); 

CREATE TABLE salaries (
    emp_no      INT             NOT NULL,
    salary      INT             NOT NULL,
    from_date   DATE            NOT NULL,
    to_date     DATE            NOT NULL,
    KEY         (emp_no),
    FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
    PRIMARY KEY (emp_no, from_date)
); 

不,我没有写这个,它可以从MySQL网站链接的第三方网站上获得。

1 个答案:

答案 0 :(得分:2)

您可能会收到CROSS JOIN又名笛卡尔产品,因为您在WHERE表上缺少dept_emp条款。< / p>