2017-12-05 19 views
0

私のアプリケーションはローン処理プラットフォームです結合テーブルの照会

この質問では、5つのテーブルがあるとします。以下は、これらのテーブルの機能と現在のレコード数の簡単な説明です。

  • ローン - ローンデータを保持している - 1.5メートルの記録
  • クライアント - クライアント名などを保持している - 500K記録
  • client_coms - クライアントの電話やメールの記録 - 1.5メートルの記録
  • クライアントアドレス - クライアントアドレスレコード - 550kレコード
  • clients_banks - クライアント銀行口座データ - 520kレコード

私は質問の下にテーブル作成クエリといくつかのテストデータを追加しました。

私の問題は、結合テーブルに保持されている値を検索するときに効率的に実行されるクエリを取得することです。

仮定のシナリオ:

  • 私はID 12516
  • をクライアントに持つ値[email protected]を持っているそのうちの一つ、このクライアントに関連するクライアント_comsテーブル内の4つのレコードがありますが、
  • 電子メールアドレスが[email protected]のクライアントがある貸出テーブルでレコードを検索する必要があります
  • 現在、clients_comテーブルをloanテーブルON client_idに参加させてから、cli 、より多くのefficiantlyこれを行うにはどのような方法があります

    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 = '[email protected]' 
    AND bank.sort_code = '309374' 
    GROUP BY l.loan_id 
    

    :これは、複数の基準は、where句

例のクエリであり、特にfを実行するためにかなりの時間がかかっている

  • をent_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, '[email protected]', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Anthony', NULL, 'Simmons', '1961-11-26', 1, 1, 1, 0, 1, 1, '2017-02-27 01:08:26'), 
        (12517, '[email protected]', '0cef1fb10f60529028a71f58e54ed07b', 'Password1!', 1, 'testme', NULL, 'testmeagain', '1983-09-19', 0, 3, 1, 0, 1, 0, '2017-04-21 08:59:16'), 
        (12518, '[email protected]', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Teddy', NULL, 'Highlander', '1990-01-01', 2, 2, 1, 0, 1, 0, '2017-04-21 09:37:08'), 
        (12519, '[email protected]', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Terry', NULL, 'Vanerslat', '1990-01-01', 2, 2, 1, 0, 1, 1, '2017-04-21 09:42:53'), 
        (12520, '[email protected]', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Testy', NULL, 'Banker', '1990-01-01', 1, 1, 1, 0, 0, 1, '2017-04-21 19:17:29'), 
        (12521, '[email protected]', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'teddy', NULL, 'kruger', '1995-07-12', 1, 1, 1, 0, 1, 1, '2017-05-01 08:23:04'), 
        (12522, '[email protected]', '0cef1fb10f60529028a71f58e54ed07b', 'Password1!', 1, 'wptest', NULL, 'wptest', '1990-01-01', 0, 3, 1, 0, 0, 0, '2017-05-02 16:51:06'), 
        (12523, '[email protected]', '0cef1fb10f60529028a71f58e54ed07b', 'Password1!', 1, 'wptest', NULL, 'wptest', '1980-01-01', 0, 2, 1, 0, 0, 1, '2017-05-08 09:46:24'), 
        (12524, '[email protected]', '16125873728ecaa5663548acd8708dc1', 'Digitalis1!', 1, 'wptester', NULL, 'wptester', '1980-01-01', 0, 3, 1, 0, 1, 1, '2017-05-08 10:09:48'), 
        (12525, '[email protected]', '7b91953d6f7ace4bd58f5e27b92129b2', 'M4ll1ng$', 2, 'test', NULL, 'test', '1988-03-15', 1, 1, 1, 0, 1, 0, '2017-05-12 10:20:18'), 
        (12526, '[email protected]', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Terry', NULL, 'Vanguard', '1998-02-03', 2, 2, 1, 0, 1, 0, '2017-05-12 21:51:34'), 
        (12527, '[email protected]', 'f44de07db359724e8be77b679d648261', 'C&e3zCyl', 2, 'Tony', NULL, 'Tester', '1998-06-16', 1, 1, 1, 0, 0, 0, '2017-05-12 22:01:57'), 
        (12528, '[email protected]', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 2, 'Terry', NULL, 'Tester', '1998-10-20', 1, 2, 1, 0, 0, 1, '2017-05-12 22:08:22'), 
        (12529, '[email protected]', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Anthony', NULL, 'Simmons', '1980-02-05', 2, 2, 1, 0, 0, 1, '2017-05-12 23:14:28'), 
        (12530, '[email protected]', '8889c4ff30a036e69af92c00e7381f9b', 'Trepalle$666', 1, 'Steve', NULL, 'Murray', '1966-02-07', 2, 2, 1, 0, 0, 0, '2017-05-16 10:05:04'), 
        (12533, '[email protected]', '7b91953d6f7ace4bd58f5e27b92129b2', 'M4ll1ng$', 1, 'Matt', NULL, 'Smith', '1970-01-27', 0, 1, 1, 0, 0, 0, '2017-05-17 10:55:23'), 
        (12534, '[email protected]', '7b91953d6f7ace4bd58f5e27b92129b2', 'M4ll1ng$', 1, 'Jacob', NULL, 'Matthews', '1960-05-31', 0, 1, 1, 0, 0, 0, '2017-05-17 15:17:56'), 
        (12535, '[email protected]', '7b91953d6f7ace4bd58f5e27b92129b2', 'M4ll1ng$', 4, 'Jess', NULL, 'Brown', '1960-06-09', 0, 2, 1, 0, 0, 0, '2017-05-17 15:45:02'), 
        (12536, '[email protected]', 'e8fec09af5441b940b200a0df09039db', 'Yamantaka108!', 1, 'Test', NULL, 'Testerly', '1997-09-22', 1, 1, 1, 0, 1, 0, '2017-05-24 05:13:46'), 
        (12539, '[email protected]', '5db95e2186901ec55d959bc0db190bc8', 'RfsBMZ_w', 1, 'Matt', NULL, 'smith', '1960-02-02', 4, 1, 1, 0, 0, 0, '2017-09-25 07:33:57'), 
        (12540, '[email protected]', 'a42316cf2aedfa853893628eb74bcfb4', 'PF8r!W&R', 4, 'Ema', NULL, 'Gupta', '1920-02-03', 1, 1, 1, 0, 1, 0, '2017-10-09 11:20:00'), 
        (12542, '[email protected]', 'f925916e2754e5e03f75dd58a5733251', '[email protected]', 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, '[email protected]', 0, 0, 0, '2017-02-27 01:08:26'), 
        (1685, 12516, 1, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 1, 1, 18, '2017-04-24 08:57:18'), 
        (1704, 12521, 1, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 1, 1, 0, '2017-05-17 10:55:23'), 
        (1749, 12533, 2, '0700', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 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, '[email protected]', 1, 1, 0, '2017-09-25 07:33:58'), 
        (1763, 12540, 1, '[email protected]', 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, '', 1, 1, 0, '2017-10-09 11:31:23'), 
        (1770, 12540, 1, '[email protected]', 0, 0, 30, '2017-10-09 13:07:24'), 
        (1771, 12542, 1, '[email protected]', 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, '[email protected]', 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, 
    
  • +0

    「LEFT JOIN」と「JOIN」(「INNER JOIN」と呼ばれることもある)の違いを理解してください。 LEFT JOINははるかに遅くなります。また、複数のクエリでこれを行うことを検討する必要があります。 'GROUP BY'との一致が複数あるテーブルから必要なデータがありません。あなたが望む出力の例は、本当に助けになります。 – Cfreak

    答えて

    関連する問題