查询连接表

时间:2017-12-05 00:46:47

标签: mysql

我的申请是贷款处理平台

对于这个问题,让我们假设有5个表。下面简要介绍了这些表的作用以及当前的记录数。

  • 贷款 - 持有贷款数据--1.5万条记录
  • 客户 - 拥有客户名称等 - 500k记录
  • client_coms - 客户电话和电子邮件记录--1.5万条记录
  • client_addresses - 客户地址记录 - 550k记录
  • clients_banks - 客户银行帐户数据 - 520k记录

我在问题的底部添加了创建表查询和一些测试数据。

我的问题是在搜索连接表中保存的值时,使查询有效运行。

假设情景:

  • 我的客户ID为12516
  • 客户端_coms表中有4条与此客户端相关的记录,其中一条记录的值为tony@findme.com
  • 我需要在贷款表中搜索记录,该表中的客户端的电子邮件地址为tony@findme.com
  • 我目前正在将clients_coms表加入到贷款表ON client_id,然后按client_id进行分组
  • 这需要花费很长时间才能运行,特别是在where子句
  • 中有多个条件

示例查询:

SELECT l.*, c.*
FROM loans l
LEFT JOIN clients c ON l.client_id = c.client_id
LEFT JOIN clients_coms com ON l.client_id = com.client_id
LEFT JOIN clients_addresses ad ON l.client_id = ad.client_id
LEFT JOIN clients_emp emp ON l.client_id = emp.client_id
LEFT JOIN clients_banks bank ON l.client_id = bank.client_id
WHERE com.value = 'llyamjamie@aol.com'
AND bank.sort_code = '309374'
GROUP BY l.loan_id

有没有办法更有效地做到这一点,最好没有GROUP BY loan_id

创建表格查询和测试数据

    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";

    CREATE TABLE `clients` (
      `client_id` int(11) NOT NULL,
      `username` varchar(150) NOT NULL,
      `password` varchar(32) NOT NULL,
      `clientref` varchar(20) NOT NULL,
      `title_id` int(11) DEFAULT NULL,
      `fname` varchar(100) DEFAULT NULL,
      `mname` varchar(100) DEFAULT NULL,
      `lname` varchar(100) DEFAULT NULL,
      `dob` date DEFAULT NULL,
      `dependants` int(3) DEFAULT NULL,
      `marital_id` int(11) DEFAULT NULL,
      `status_difficulty_id` int(11) NOT NULL DEFAULT '1',
      `blacklisted` tinyint(1) NOT NULL DEFAULT '0',
      `market_consent` tinyint(1) NOT NULL DEFAULT '0',
      `market_broker` tinyint(1) NOT NULL DEFAULT '0',
      `created` timestamp NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO `clients` (`client_id`, `username`, `password`, `clientref`, `title_id`, `fname`, `mname`, `lname`, `dob`, `dependants`, `marital_id`, `status_difficulty_id`, `blacklisted`, `market_consent`, `market_broker`, `created`) VALUES
    (12516, 'tony@strat55yinternet.com', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Anthony', NULL, 'Simmons', '1961-11-26', 1, 1, 1, 0, 1, 1, '2017-02-27 01:08:26'),
    (12517, 'trythis@ungdfgdfg.co.uk', '0cef1fb10f60529028a71f58e54ed07b', 'Password1!', 1, 'testme', NULL, 'testmeagain', '1983-09-19', 0, 3, 1, 0, 1, 0, '2017-04-21 08:59:16'),
    (12518, 'teddy.highlander@stradfgdfnternet.com', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Teddy', NULL, 'Highlander', '1990-01-01', 2, 2, 1, 0, 1, 0, '2017-04-21 09:37:08'),
    (12519, 'terry.v@stradfgyinternet.com', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Terry', NULL, 'Vanerslat', '1990-01-01', 2, 2, 1, 0, 1, 1, '2017-04-21 09:42:53'),
    (12520, 'testybanker@stratedfgets.com', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Testy', NULL, 'Banker', '1990-01-01', 1, 1, 1, 0, 0, 1, '2017-04-21 19:17:29'),
    (12521, 'teddy.kruger@stradfgrnet.com', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'teddy', NULL, 'kruger', '1995-07-12', 1, 1, 1, 0, 1, 1, '2017-05-01 08:23:04'),
    (12522, 'test123@test.com', '0cef1fb10f60529028a71f58e54ed07b', 'Password1!', 1, 'wptest', NULL, 'wptest', '1990-01-01', 0, 3, 1, 0, 0, 0, '2017-05-02 16:51:06'),
    (12523, 'test123@undfggk.co.uk', '0cef1fb10f60529028a71f58e54ed07b', 'Password1!', 1, 'wptest', NULL, 'wptest', '1980-01-01', 0, 2, 1, 0, 0, 1, '2017-05-08 09:46:24'),
    (12524, '43201at78285n316xl@udgfdfgdf.co.uk', '16125873728ecaa5663548acd8708dc1', 'Digitalis1!', 1, 'wptester', NULL, 'wptester', '1980-01-01', 0, 3, 1, 0, 1, 1, '2017-05-08 10:09:48'),
    (12525, 'test@test.com', '7b91953d6f7ace4bd58f5e27b92129b2', 'M4ll1ng$', 2, 'test', NULL, 'test', '1988-03-15', 1, 1, 1, 0, 1, 0, '2017-05-12 10:20:18'),
    (12526, 'terry@terry.com', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Terry', NULL, 'Vanguard', '1998-02-03', 2, 2, 1, 0, 1, 0, '2017-05-12 21:51:34'),
    (12527, 'tony@tester.com', 'f44de07db359724e8be77b679d648261', 'C&e3zCyl', 2, 'Tony', NULL, 'Tester', '1998-06-16', 1, 1, 1, 0, 0, 0, '2017-05-12 22:01:57'),
    (12528, 'terry@tester.com', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 2, 'Terry', NULL, 'Tester', '1998-10-20', 1, 2, 1, 0, 0, 1, '2017-05-12 22:08:22'),
    (12529, 'info@myemail.com', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Anthony', NULL, 'Simmons', '1980-02-05', 2, 2, 1, 0, 0, 1, '2017-05-12 23:14:28'),
    (12530, 'sgmudfgrray@lidfgve.co.uk', '8889c4ff30a036e69af92c00e7381f9b', 'Trepalle$666', 1, 'Steve', NULL, 'Murray', '1966-02-07', 2, 2, 1, 0, 0, 0, '2017-05-16 10:05:04'),
    (12533, 'mbucgper@undfgck.co.uk', '7b91953d6f7ace4bd58f5e27b92129b2', 'M4ll1ng$', 1, 'Matt', NULL, 'Smith', '1970-01-27', 0, 1, 1, 0, 0, 0, '2017-05-17 10:55:23'),
    (12534, '1@unerteruck.co.uk', '7b91953d6f7ace4bd58f5e27b92129b2', 'M4ll1ng$', 1, 'Jacob', NULL, 'Matthews', '1960-05-31', 0, 1, 1, 0, 0, 0, '2017-05-17 15:17:56'),
    (12535, '2@uncrteterk.co.uk', '7b91953d6f7ace4bd58f5e27b92129b2', 'M4ll1ng$', 4, 'Jess', NULL, 'Brown', '1960-06-09', 0, 2, 1, 0, 0, 0, '2017-05-17 15:45:02'),
    (12536, 'test@straerttester.com', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Test', NULL, 'Testerly', '1997-09-22', 1, 1, 1, 0, 1, 0, '2017-05-24 05:13:46'),
    (12539, 'test@test34324.com', '5db95e2186901ec55d959bc0db190bc8', 'RfsBMZ_w', 1, 'Matt', NULL, 'smith', '1960-02-02', 4, 1, 1, 0, 0, 0, '2017-09-25 07:33:57'),
    (12540, 'test@gmail.com', 'a42316cf2aedfa853893628eb74bcfb4', 'PF8r!W&R', 4, 'Ema', NULL, 'Gupta', '1920-02-03', 1, 1, 1, 0, 1, 0, '2017-10-09 11:20:00'),
    (12542, 'rgupta@unertetck.com', 'f925916e2754e5e03f75dd58a5733251', 'Test@123', 3, 'Ruchi  ', NULL, 'Gupta', '1983-01-01', 0, 1, 1, 0, 1, 0, '2017-10-09 13:09:49');


    CREATE TABLE `clients_addresses` (
      `client_address_id` int(11) NOT NULL,
      `client_id` int(11) NOT NULL,
      `house_number` varchar(20) DEFAULT NULL,
      `house_name` varchar(50) DEFAULT NULL,
      `street` varchar(100) DEFAULT NULL,
      `city` varchar(50) DEFAULT NULL,
      `county` varchar(70) DEFAULT NULL,
      `postcode` varchar(10) DEFAULT NULL,
      `home_status_id` int(11) DEFAULT NULL,
      `movein_date` date DEFAULT NULL,
      `current` tinyint(1) NOT NULL DEFAULT '1',
      `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


    INSERT INTO `clients_addresses` (`client_address_id`, `client_id`, `house_number`, `house_name`, `street`, `city`, `county`, `postcode`, `home_status_id`, `movein_date`, `current`, `created`) VALUES
    (590, 12516, '1', 'Paddock House', 'Merrow Street', 'Guildford', NULL, 'GU47AG', 1, '2000-02-01', 1, '2017-02-27 01:09:17'),
    (591, 12519, '45', 'willow tree', 'smith street', 'southfields', NULL, 'Sw185dj', 1, '2000-01-01', 1, '2017-04-21 09:43:44'),
    (592, 12520, '44', 'Patcick house', 'pastel St', 'Guildford', NULL, 'Gu47ag', 1, '1998-01-01', 1, '2017-04-21 19:18:16'),
    (593, 12521, '1', 'Haybarn House', 'Guildford Rd', 'Guildford', NULL, 'GU12QL', 1, '1998-01-01', 1, '2017-05-01 08:23:43'),
    (594, 12522, '221b Baker Street', '', 'Baker Street', 'bromley', NULL, 'NW1 6XE', 1, '2005-12-01', 1, '2017-05-02 16:52:46'),
    (595, 12523, '5-6', '', 'Matthews Street', 'Rugby', NULL, 'CV21 3BY', 1, '2001-01-01', 1, '2017-05-08 09:47:58'),
    (596, 12524, '21', '', 'Henry Street', 'Stratford', NULL, 'CV37 6QW', 1, '2000-12-01', 1, '2017-05-08 10:11:16'),
    (597, 12525, '707', '', 'Churchill Way', 'Westerham', NULL, 'TN16 3BN', 1, '2008-04-01', 1, '2017-05-12 10:21:00'),
    (598, 12528, '58', '', 'High Path Road', 'Guildford', NULL, 'GU1 2QL', 1, '2017-01-01', 1, '2017-05-12 22:11:46'),
    (599, 12528, '', '2 Paddock House', 'Merrow Street', 'Guildford', NULL, 'GU4 7AG', 2, '2016-01-01', 0, '2017-05-12 22:11:46'),
    (600, 12529, '3', 'Britannia House', 'Caerphilly Business Park', 'Caerphilly', NULL, 'CF83 3GG', 2, '2012-02-01', 1, '2017-05-12 23:14:46'),
    (601, 12530, '1', 'Cronks Farm', 'Hampstead Lane', 'Maidstone', NULL, 'ME185HN', 1, '2004-04-01', 1, '2017-05-16 10:06:27'),
    (604, 12542, '707', '', 'Biggnin Hill', 'westerham', NULL, 'TN16 3BN', 1, '2011-01-01', 1, '2017-10-09 13:10:42');


    CREATE TABLE `clients_banks` (
      `client_bank_id` int(11) NOT NULL,
      `client_id` int(11) NOT NULL,
      `bank_id` int(11) DEFAULT NULL,
      `sort_code` varchar(6) DEFAULT NULL,
      `account` varchar(8) DEFAULT NULL,
      `bank_name` varchar(70) DEFAULT NULL COMMENT 'Returned by AFD API',
      `active` tinyint(1) NOT NULL DEFAULT '1',
      `created` timestamp NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


    INSERT INTO `clients_banks` (`client_bank_id`, `client_id`, `bank_id`, `sort_code`, `account`, `bank_name`, `active`, `created`) VALUES
    (544, 12516, 3, '309374', '03522844', 'Lloyds Bank PLC', 0, '2017-02-27 01:11:47'),
    (551, 12516, 3, '301641', '60722260', 'Lloyds International', 1, '2017-02-28 06:16:41'),
    (552, 12516, 3, '309384', '03544844', 'Lloyds Bank PLC', 0, '2017-02-28 06:17:57'),
    (553, 12519, 3, '804378', '03555844', 'Lloyds Bank PLC', 1, '2017-04-21 09:52:12'),
    (554, 12520, 3, '309355', '03555844', 'Lloyds Bank PLC', 1, '2017-04-21 19:21:09'),
    (555, 12521, 3, '802374', '03566844', 'Lloyds Bank PLC', 1, '2017-05-01 08:37:04'),
    (556, 12523, 3, '602044', '00000000', 'Nat West Bank PLC', 1, '2017-05-08 09:54:26'),
    (557, 12524, 3, '902046', '00000000', 'Nat West Bank PLC', 1, '2017-05-08 10:15:03'),
    (558, 12525, 5, '602044', '00000000', 'Nat West Bank PLC', 1, '2017-05-12 11:42:42'),
    (559, 12528, 3, '309674', '03577844', 'Lloyds Bank PLC', 1, '2017-05-12 22:15:12'),
    (560, 12529, 3, '399379', '03588844', 'Lloyds Bank PLC', 0, '2017-05-12 23:18:40'),
    (561, 12529, 3, '909374', '03511844', 'Lloyds Bank PLC', 1, '2017-05-12 23:25:53');


    CREATE TABLE `clients_coms` (
      `client_coms_id` int(11) NOT NULL,
      `client_id` int(11) NOT NULL,
      `coms_id` int(11) NOT NULL,
      `value` varchar(150) NOT NULL,
      `active` tinyint(1) NOT NULL DEFAULT '1',
      `main` tinyint(1) NOT NULL DEFAULT '1',
      `user_id` int(11) NOT NULL,
      `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    INSERT INTO `clients_coms` (`client_coms_id`, `client_id`, `coms_id`, `value`, `active`, `main`, `user_id`, `created`) VALUES
    (1685, 12516, 1, 'tony@findme.com', 0, 0, 0, '2017-02-27 01:08:26'),
    (1685, 12516, 1, 'tony@findmeagain.com', 0, 0, 0, '2017-02-27 01:08:26'),
    (1686, 12516, 2, '07081447123', 1, 1, 0, '2017-02-27 01:08:26'),
    (1687, 12516, 3, '02081447423', 1, 1, 0, '2017-02-27 01:08:26'),
    (1688, 12516, 4, '02081447128', 1, 1, 0, '2017-02-27 01:10:08'),
    (1689, 12517, 1, 'trythis@sdfsdfsdf.co.uk', 0, 1, 0, '2017-04-21 08:59:16'),
    (1690, 12517, 2, '07702419620', 0, 1, 0, '2017-04-21 08:59:16'),
    (1691, 12517, 3, '07702019420', 0, 1, 0, '2017-04-21 08:59:16'),
    (1692, 12518, 1, 'teddy.highlander@sdfsdfsdf.com', 1, 1, 0, '2017-04-21 09:37:08'),
    (1693, 12518, 2, '07232344566', 1, 1, 0, '2017-04-21 09:37:08'),
    (1694, 12518, 3, '02928848888', 1, 1, 0, '2017-04-21 09:37:08'),
    (1695, 12519, 1, 'terry.v@sdfsdfsd.com', 1, 1, 0, '2017-04-21 09:42:53'),
    (1696, 12519, 2, '07332242323', 1, 1, 0, '2017-04-21 09:42:53'),
    (1697, 12519, 3, '02048787656', 1, 1, 0, '2017-04-21 09:42:53'),
    (1698, 12519, 4, '02089448776', 1, 1, 0, '2017-04-21 09:46:45'),
    (1699, 12520, 1, 'testybanker@sdfsd.com', 1, 1, 0, '2017-04-21 19:17:29'),
    (1700, 12520, 2, '07224232345', 1, 1, 0, '2017-04-21 19:17:29'),
    (1701, 12520, 3, '02083438383', 1, 1, 0, '2017-04-21 19:17:29'),
    (1702, 12520, 4, '02082384282', 1, 1, 0, '2017-04-21 19:19:00'),
    (1703, 12516, 1, 'tday@sdfsdfsdfs.co.uk', 1, 1, 18, '2017-04-24 08:57:18'),
    (1704, 12521, 1, 'teddy.kruger@sdfsdfsd.com', 1, 1, 0, '2017-05-01 08:23:04'),
    (1705, 12521, 2, '07665865665', 1, 1, 0, '2017-05-01 08:23:04'),
    (1706, 12521, 3, '02087278888', 1, 1, 0, '2017-05-01 08:23:04'),
    (1707, 12521, 4, '02098828722', 1, 1, 0, '2017-05-01 08:24:26'),
    (1708, 12522, 1, 'test123@test.com', 1, 1, 0, '2017-05-02 16:51:06'),
    (1709, 12522, 2, '07507254489', 1, 1, 0, '2017-05-02 16:51:06'),
    (1710, 12522, 3, '07207254489', 1, 1, 0, '2017-05-02 16:51:06'),
    (1711, 12522, 4, '01522904210', 1, 1, 0, '2017-05-02 16:55:13'),
    (1712, 12523, 1, 'test123@fdgdfg.co.uk', 1, 1, 0, '2017-05-08 09:46:24'),
    (1713, 12523, 2, '07494254592', 1, 1, 0, '2017-05-08 09:46:24'),
    (1714, 12523, 3, '07494254592', 1, 1, 0, '2017-05-08 09:46:24'),
    (1715, 12523, 4, '01980512110', 1, 1, 0, '2017-05-08 09:49:29'),
    (1716, 12524, 1, '43201at78285n316xl@undgdfck.co.uk', 1, 1, 0, '2017-05-08 10:09:48'),
    (1717, 12524, 2, '07494854590', 1, 1, 0, '2017-05-08 10:09:48'),
    (1718, 12524, 3, '01420414510', 1, 1, 0, '2017-05-08 10:09:48'),
    (1719, 12524, 4, '07494854592', 1, 1, 0, '2017-05-08 10:13:02'),
    (1720, 12525, 1, 'test@test.com', 1, 1, 0, '2017-05-12 10:20:18'),
    (1721, 12525, 2, '07343242342', 1, 1, 0, '2017-05-12 10:20:18'),
    (1722, 12525, 3, '01731123456', 1, 1, 0, '2017-05-12 10:20:18'),
    (1723, 12525, 4, '01734123456', 1, 1, 0, '2017-05-12 10:21:49'),
    (1724, 12526, 1, 'terry@terry.com', 1, 1, 0, '2017-05-12 21:51:34'),
    (1725, 12526, 2, '07123333333', 1, 1, 0, '2017-05-12 21:51:34'),
    (1726, 12526, 3, '03034334343', 1, 1, 0, '2017-05-12 21:51:34'),
    (1727, 12527, 1, 'tony@tester.com', 1, 0, 0, '2017-05-12 22:01:57'),
    (1728, 12527, 2, '07544444444', 1, 1, 0, '2017-05-12 22:01:57'),
    (1729, 12527, 3, '02086767676', 1, 1, 0, '2017-05-12 22:01:57'),
    (1730, 12528, 1, 'terry@tester.com', 1, 1, 0, '2017-05-12 22:08:22'),
    (1731, 12528, 2, '07332232222', 1, 1, 0, '2017-05-12 22:08:22'),
    (1732, 12528, 3, '02098888888', 1, 1, 0, '2017-05-12 22:08:22'),
    (1733, 12528, 4, '02089992929', 1, 1, 0, '2017-05-12 22:13:02'),
    (1734, 12529, 1, 'info@myemail.com', 1, 1, 0, '2017-05-12 23:14:28'),
    (1735, 12529, 2, '07086667656', 1, 1, 0, '2017-05-12 23:14:28'),
    (1736, 12529, 3, '02098887878', 1, 1, 0, '2017-05-12 23:14:28'),
    (1737, 12529, 4, '02087776767', 1, 1, 0, '2017-05-12 23:15:17'),
    (1738, 12530, 1, 'sgmudfgrray@live.co.uk', 1, 1, 0, '2017-05-16 10:05:04'),
    (1739, 12530, 2, '07771755011', 1, 1, 0, '2017-05-16 10:05:04'),
    (1740, 12530, 3, '01622817656', 1, 1, 0, '2017-05-16 10:05:04'),
    (1741, 12530, 4, '02037475718', 1, 1, 0, '2017-05-16 10:07:43'),
    (1748, 12533, 1, 'mbudfgper@undfgdk.co.uk', 1, 1, 0, '2017-05-17 10:55:23'),
    (1749, 12533, 2, '07000123456', 1, 1, 0, '2017-05-17 10:55:23'),
    (1750, 12533, 3, '02030000000', 1, 1, 0, '2017-05-17 10:55:23'),
    (1751, 12534, 1, '1@uncdfgdfgck.co.uk', 1, 1, 0, '2017-05-17 15:17:56'),
    (1752, 12534, 2, '07000000000', 1, 1, 0, '2017-05-17 15:17:56'),
    (1753, 12534, 3, '02000000000', 1, 1, 0, '2017-05-17 15:17:56'),
    (1754, 12535, 1, '2@uncldfgdfk.co.uk', 1, 1, 0, '2017-05-17 15:45:02'),
    (1755, 12535, 2, '07000000001', 1, 1, 0, '2017-05-17 15:45:02'),
    (1756, 12535, 3, '02030000001', 1, 1, 0, '2017-05-17 15:45:02'),
    (1757, 12536, 1, 'test@stra890tedfgster.com', 1, 1, 0, '2017-05-24 05:13:46'),
    (1758, 12536, 2, '07332232277', 1, 1, 0, '2017-05-24 05:13:46'),
    (1759, 12536, 3, '02998879888', 1, 1, 0, '2017-05-24 05:13:46'),
    (1760, 12539, 1, 'test@test34324.com', 1, 1, 0, '2017-09-25 07:33:58'),
    (1763, 12540, 1, 'test@gmail.com', 1, 1, 0, '2017-10-09 11:20:00'),
    (1764, 12540, 2, '07123456789', 1, 1, 0, '2017-10-09 11:20:00'),
    (1765, 12540, 3, '01123456789', 1, 1, 0, '2017-10-09 11:20:00'),
    (1766, 12540, 4, '01234567895', 1, 1, 0, '2017-10-09 11:31:23'),
    (1770, 12540, 1, 'gargruchi12@gmail.com', 0, 0, 30, '2017-10-09 13:07:24'),
    (1771, 12542, 1, 'rgupta@uddfgdfgk.com', 1, 1, 0, '2017-10-09 13:09:49'),
    (1772, 12542, 2, '07507245489', 1, 1, 0, '2017-10-09 13:09:49'),
    (1773, 12542, 3, '01282560110', 1, 1, 0, '2017-10-09 13:09:49'),
    (1774, 12542, 4, '01282560110', 1, 1, 0, '2017-10-09 13:11:48'),
    (1775, 12542, 4, '01324564564', 1, 1, 0, '2017-10-09 13:14:34'),
    (1776, 12527, 1, 'rob@nedgfdfgdls.co.uk', 1, 1, 31, '2017-10-12 09:42:13');


    CREATE TABLE `loans` (
      `loan_id` int(11) NOT NULL,
      `client_id` int(11) NOT NULL,
      `product_id` int(11) NOT NULL DEFAULT '1',
      `amount` decimal(7,2) NOT NULL DEFAULT '0.00',
      `interest_percent` decimal(7,2) DEFAULT '0.00' COMMENT 'Daily Interest Rate',
      `instalments` int(3) DEFAULT NULL,
      `fraud_id` int(11) NOT NULL DEFAULT '1',
      `code` varchar(4) NOT NULL,
      `code_sent` tinyint(1) NOT NULL DEFAULT '0',
      `signed` tinyint(1) DEFAULT '0',
      `signed_date` date DEFAULT NULL,
      `approved_amount` decimal(7,2) NOT NULL DEFAULT '0.00',
      `approved_user_id` int(11) DEFAULT NULL,
      `rtd` tinyint(1) NOT NULL DEFAULT '0',
      `rtd_date` datetime DEFAULT NULL,
      `deposited_amount` decimal(7,2) NOT NULL DEFAULT '0.00',
      `deposited_date` date DEFAULT NULL,
      `depsosited_user_id` int(11) DEFAULT NULL,
      `due_date` date DEFAULT NULL,
      `assigned_user_id` int(11) DEFAULT '0',
      `content_message_id` int(11) DEFAULT NULL,
      `balance` decimal(7,2) DEFAULT NULL,
      `payout` decimal(7,2) DEFAULT NULL,
      `repaid` tinyint(1) NOT NULL DEFAULT '0',
      `repaid_date` date DEFAULT NULL,
      `writeoff` tinyint(1) NOT NULL DEFAULT '0',
      `writeoff_date` date DEFAULT NULL,
      `writeoff_user_id` int(11) DEFAULT NULL,
      `apr` decimal(10,2) DEFAULT NULL,
      `sweep` tinyint(1) NOT NULL DEFAULT '1',
      `test` tinyint(1) NOT NULL DEFAULT '0',
      `ip` varchar(20) DEFAULT NULL,
      `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO `loans` (`loan_id`, `client_id`, `product_id`, `amount`, `interest_percent`, `instalments`, `fraud_id`, `code`, `code_sent`, `signed`, `signed_date`, `approved_amount`, `approved_user_id`, `rtd`, `rtd_date`, `deposited_amount`, `deposited_date`, `depsosited_user_id`, `due_date`, `assigned_user_id`, `content_message_id`, `balance`, `payout`, `repaid`, `repaid_date`, `writeoff`, `writeoff_date`, `writeoff_user_id`, `apr`, `sweep`, `test`, `ip`, `created`) VALUES
    (9549, 12516, 4, '200.00', '0.80', 6, 1, '7812', 0, 1, '2017-02-27', '200.00', 1, 0, NULL, '200.00', '2017-01-03', 1, NULL, 1, 4, '90.22', '90.22', 0, NULL, 0, NULL, NULL, '1266.04', 1, 0, NULL, '2017-02-27 01:08:26'),
    (9550, 12517, 4, '100.00', '0.80', 6, 1, '7521', 0, 0, NULL, '0.00', NULL, 0, NULL, '0.00', NULL, NULL, NULL, 18, NULL, '0.00', NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-04-21 08:59:16'),
    (9551, 12518, 4, '100.00', '0.80', 6, 1, '7146', 0, 0, NULL, '0.00', NULL, 0, NULL, '0.00', NULL, NULL, NULL, 0, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-04-21 09:37:08'),
    (9552, 12519, 4, '100.00', '0.80', 6, 1, '9524', 0, 0, NULL, '0.00', 0, 0, NULL, '0.00', NULL, NULL, NULL, 0, 2, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-04-21 09:42:53'),
    (9553, 12520, 1, '100.00', '0.80', 3, 1, '2597', 0, 0, NULL, '0.00', 0, 0, NULL, '0.00', NULL, NULL, NULL, 0, 2, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-04-21 19:17:29'),
    (9554, 12521, 1, '100.00', '0.80', 3, 1, '8057', 0, 0, NULL, '0.00', 0, 0, NULL, '0.00', NULL, NULL, NULL, 0, 2, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-01 08:23:04'),
    (9555, 12516, 4, '200.00', '0.80', 6, 1, '7812', 0, 1, '2017-03-27', '200.00', 1, 0, NULL, '200.00', '2017-02-03', 1, NULL, 1, 4, '508.80', NULL, 0, NULL, 0, NULL, NULL, '1266.04', 1, 0, NULL, '2017-03-27 00:08:26'),
    (9556, 12522, 1, '100.00', '0.80', 3, 1, '2976', 0, 0, NULL, '0.00', NULL, 0, NULL, '0.00', NULL, NULL, NULL, 0, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-02 16:51:06'),
    (9557, 12523, 1, '100.00', '0.80', 3, 1, '4366', 0, 0, NULL, '0.00', 0, 0, NULL, '0.00', NULL, NULL, NULL, 0, 2, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-08 09:46:24'),
    (9558, 12524, 1, '100.00', '0.80', 3, 1, '9040', 0, 0, NULL, '100.00', 18, 0, NULL, '0.00', NULL, NULL, NULL, 0, 2, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-08 10:09:48'),
    (9559, 12525, 1, '250.00', '0.80', 3, 1, '4416', 0, 0, NULL, '0.00', 0, 0, NULL, '0.00', NULL, NULL, NULL, 0, 2, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-12 10:20:18'),
    (9560, 12526, 1, '175.00', '0.80', 3, 1, '2242', 0, 0, NULL, '0.00', NULL, 0, NULL, '0.00', NULL, NULL, NULL, 0, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-12 21:51:34'),
    (9561, 12527, 1, '100.00', '0.80', 3, 1, '2623', 0, 0, NULL, '0.00', NULL, 0, NULL, '0.00', NULL, NULL, NULL, 0, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-12 22:01:57'),
    (9562, 12528, 1, '100.00', '0.80', 3, 1, '4932', 1, 1, '2017-05-12', '100.00', 1, 1, '2017-05-13 05:14:06', '0.00', NULL, NULL, NULL, 0, 2, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-12 22:08:22'),
    (9563, 12529, 1, '150.00', '0.80', 3, 1, '8136', 0, 0, NULL, '0.00', 0, 0, NULL, '0.00', NULL, NULL, NULL, 0, 2, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-12 23:14:28'),
    (9564, 12530, 1, '350.00', '0.80', 3, 1, '2248', 0, 0, NULL, '0.00', 0, 0, NULL, '0.00', NULL, NULL, NULL, 0, 2, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-16 10:05:04'),
    (9565, 12536, 1, '200.00', '0.80', 3, 1, '8079', 0, 0, NULL, '0.00', NULL, 0, NULL, '0.00', NULL, NULL, NULL, 0, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-05-24 05:13:46'),
    (9567, 12539, 1, '300.00', '0.80', 3, 1, '8223', 0, 0, NULL, '0.00', NULL, 0, NULL, '0.00', NULL, NULL, NULL, 0, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-09-25 07:33:58'),
    (9568, 12540, 1, '375.00', '0.80', 3, 1, '9123', 0, 0, NULL, '0.00', 0, 0, NULL, '0.00', NULL, NULL, NULL, 0, 2, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-10-09 11:20:00'),
    (9570, 12542, 1, '100.00', '0.80', 3, 1, '4470', 0, 0, NULL, '0.00', NULL, 0, NULL, '0.00', NULL, NULL, NULL, 0, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL, 1, 0, NULL, '2017-10-09 13:09:49');

    ALTER TABLE `clients`
      ADD PRIMARY KEY (`client_id`),
      ADD KEY `fname` (`fname`),
      ADD KEY `created` (`created`),
      ADD KEY `lname` (`lname`),
      ADD KEY `title_id` (`title_id`),
      ADD KEY `username` (`username`),
      ADD KEY `clientref` (`clientref`),
      ADD KEY `dob` (`dob`),
      ADD KEY `market_consent` (`market_consent`),
      ADD KEY `blacklisted` (`blacklisted`),
      ADD KEY `market_broker` (`market_broker`),
      ADD KEY `marital_id` (`marital_id`),
      ADD KEY `status_difficulty_id` (`status_difficulty_id`);

    ALTER TABLE `clients_addresses`
      ADD PRIMARY KEY (`client_address_id`),
      ADD KEY `created` (`created`),
      ADD KEY `postcode` (`postcode`),
      ADD KEY `client_id` (`client_id`),
      ADD KEY `move_in_date` (`movein_date`),
      ADD KEY `current` (`current`),
      ADD KEY `home_status_id` (`home_status_id`);

    ALTER TABLE `clients_banks`
      ADD PRIMARY KEY (`client_bank_id`),
      ADD KEY `created` (`created`),
      ADD KEY `sort_code` (`sort_code`),
      ADD KEY `bank_id` (`bank_id`),
      ADD KEY `client_id` (`client_id`),
      ADD KEY `active` (`active`);

    ALTER TABLE `clients_coms`
      ADD PRIMARY KEY (`client_coms_id`),
      ADD KEY `client_id` (`client_id`),
      ADD KEY `value` (`value`),
      ADD KEY `active` (`active`),
      ADD KEY `created` (`created`),
      ADD KEY `coms_id` (`coms_id`),
      ADD KEY `primary_2` (`main`),
      ADD KEY `user_id` (`user_id`);

    ALTER TABLE `loans`
      ADD PRIMARY KEY (`loan_id`),
      ADD KEY `client_id` (`client_id`),
      ADD KEY `product_id` (`product_id`),
      ADD KEY `depsoited_user_id` (`depsosited_user_id`),
      ADD KEY `deposited_date` (`deposited_date`),
      ADD KEY `approved_user_id` (`approved_user_id`),
      ADD KEY `deposited_amount` (`deposited_amount`),
      ADD KEY `created` (`created`),
      ADD KEY `repaid` (`repaid`),
      ADD KEY `repaid_date` (`repaid_date`),
      ADD KEY `sweep` (`sweep`),
      ADD KEY `due_date` (`due_date`),
      ADD KEY `ip` (`ip`),
      ADD KEY `signed` (`signed`),
      ADD KEY `signed_date` (`signed_date`),
      ADD KEY `fraud_id` (`fraud_id`),
      ADD KEY `assigned_user_id` (`assigned_user_id`),
      ADD KEY `code_sent` (`code_sent`),
      ADD KEY `test` (`test`),
      ADD KEY `content_message_id` (`content_message_id`),
      ADD KEY `rtd` (`rtd`),
      ADD KEY `rtd_date` (`rtd_date`),
      ADD KEY `rtd_2` (`rtd`,`deposited_amount`),
      ADD KEY `writeoff` (`writeoff`),
      ADD KEY `writeoff_date` (`writeoff_date`),
      ADD KEY `payout` (`payout`),
      ADD KEY `balance` (`balance`),
      ADD KEY `writeoff_user_id` (`writeoff_user_id`);


    ALTER TABLE `clients`
      MODIFY `client_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12543;

    ALTER TABLE `clients_addresses`
      MODIFY `client_address_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=605;

    ALTER TABLE `clients_banks`
      MODIFY `client_bank_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=564;

    ALTER TABLE `clients_coms`
      MODIFY `client_coms_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1777;

    ALTER TABLE `loans`
      MODIFY `loan_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9571;

    ALTER TABLE `clients_addresses`
      ADD CONSTRAINT `clients_addresses_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `clients` (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE,

    ALTER TABLE `clients_banks`
      ADD CONSTRAINT `clients_banks_ibfk_2` FOREIGN KEY (`client_id`) REFERENCES `clients` (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE;

    ALTER TABLE `clients_coms`
      ADD CONSTRAINT `clients_coms_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `clients` (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE,

    ALTER TABLE `loans`
      ADD CONSTRAINT `loans_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `clients` (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE,

2 个答案:

答案 0 :(得分:1)

在标准SQL中,不可能按单列进行分组,仍然可以输出2个表(SELECT l.*, c.*)中的列。 MySQL contains a non-standard "extension"允许您的查询在没有错误消息的情况下运行,但事实是您不应该依赖该扩展。因此,您需要对使用group by的任何查询采用更严格的方法。

您还使用了一系列LEFT JOINS,我们使用该类型连接的原因是仍然可以返回主表中不匹配的行。换句话说,对于每个左连接表,表的列都可以为NULL。但是,你的where子句覆盖了这种可能性,坚持com.value = 'llyamjamie@aol.com'bank.sort_code = '309374'。如果您想要更高效率,请不要在这些表格中使用左连接。

看起来好像你想要一个查询几乎做任何事情,同时达到最高效率。然而,这两个目标(灵活性和效率)竞争。

您可能会发现以下查询样式更有效但又更灵活。我不知道你是如何构建查询的,但我认为你有一组参数,所以下面我只有一些占位符可用于你可能正在使用的任何东西。

select l.*, c.* ## but specify the columns you actually need
from Loans l
inner join clients c ON l.client_id = c.client_id
where l.client_id IN (
   select client_id
   from clients where @clients is not null
   union all
   select client_id
   from clients_coms where clients_coms.value = 'llyamjamie@aol.com'
   union all
   select client_id
   from clients_addresses where @clients_addresses is not null
   union all
   select client_id
   from clients_emp where @clients_emp is not null
   union all
   select client_id
   from clients_banks where clients_banks.sort_code = '309374'
  )

理想情况下,如果可以,您将省略该查询的冗余部分。请注意,我们不需要通过此方法进行分组。但请注意,此查询具有ORs即

的效果
`com.value = 'llyamjamie@aol.com'` OR `bank.sort_code = '309374'`

然后,您需要了解如何为所有这些表编制索引。用于连接表的每一列都将被编入索引,我信任。对于其他索引,您需要查看解释计划,但最常搜索的列是索引的候选者。

答案 1 :(得分:0)

您的查询类似于选择所有表格字段:

<强>低效:

  1. 在显示您想要的字段之前包含所有记录。
  2. 当你获得数百万的数据时,它会使你的查询变得太慢。
  3. 性能问题。
  4. 要解决:

    只需选择您需要的特定字段:

    避免使用Select *

    select a.f1,a.f2,b.f3,a.f4,b.f5 from
    (select f1,f2,f4 from table1) as a
    left join
    (select f1, f3,f5 from table2) as b
    on a.f1 = b.f1
    

    至于我,这被称为表query optimizing

    关闭课程所有indexes表格均为donecreated

相关问题