{"id":818,"date":"2022-04-08T10:14:32","date_gmt":"2022-04-08T03:14:32","guid":{"rendered":"https:\/\/www.bagi2info.com\/?p=818"},"modified":"2023-03-05T13:54:39","modified_gmt":"2023-03-05T06:54:39","slug":"tips-tips-sql","status":"publish","type":"post","link":"https:\/\/www.bagi2info.com\/en\/sql-tips-trick\/","title":{"rendered":"SQL query optimization tips with index, subquery, and cross join"},"content":{"rendered":"<p><a href=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2015\/11\/2017-10-02_07h22_12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1032\" src=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2015\/11\/2017-10-02_07h22_12-1024x579.png\" alt=\"\" width=\"1024\" height=\"579\" srcset=\"https:\/\/www.bagi2info.com\/wp-content\/uploads\/2015\/11\/2017-10-02_07h22_12-1024x579.png 1024w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2015\/11\/2017-10-02_07h22_12-300x170.png 300w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2015\/11\/2017-10-02_07h22_12-768x434.png 768w, https:\/\/www.bagi2info.com\/wp-content\/uploads\/2015\/11\/2017-10-02_07h22_12.png 1423w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>1. Use numeric data type as primary key or foreign key<\/p>\n<p>Often beginners will design tables using string types as keys, and this will result in future performance when the data is large, so it has implications for table changes and coding, and this is sometimes difficult because if the system has been implemented the database is already filled with data. data.<\/p>\n<p>The following example is a student table for course grades<\/p>\n<blockquote><p><strong>tb_mhs&nbsp;<\/strong><br \/>\nid int auto increment primary key<br \/>\nnik varchar(50)<br \/>\nnama varchar(250)<br \/>\ndob datetime<br \/>\ngender varchat(1)<\/p><\/blockquote>\n<blockquote><p><strong>tb_matakuliah<\/strong><br \/>\nid&nbsp; int auto increment primary key<br \/>\nketerangan&nbsp;varchar(250)<br \/>\nsks int<\/p><\/blockquote>\n<blockquote><p><strong>tb_nilai<\/strong><br \/>\nno&nbsp; int auto increment primary key<br \/>\nid_mhs int<br \/>\nid_matakuliah int<br \/>\nnilai float<\/p><\/blockquote>\n<p>Pay attention to the tb_value table which is a connecting relation from the tb_mhs and tb_matasiswa tables, the data type used is integer in the id_mhs and id_matasiswa columns.<\/p>\n<h2>2. Subquery function to shorten the data retrieval process<\/h2>\n<p>Contoh<\/p>\n<blockquote><p>SELECT m.id, m.nik, m.nama, m.dob, m.gender<br \/>\nFROM tb_mhs m<br \/>\nJOIN tb_nilai n ON m.id = n.id_mhs<br \/>\nJOIN tb_matakuliah mk ON n.id_matakuliah = mk.id<\/p><\/blockquote>\n<p>By using subquery we can shorten data retrieval on join process with tb_value,<br \/>\ninstead of using join and scanning the entire table on tb_value, we can use<br \/>\nsubquery by summarizing scan data<\/p>\n<blockquote><p>SELECT m.id, m.nik, m.nama, m.dob, m.gender<br \/>\nFROM tb_mhs m<br \/>\nJOIN (SELECT * FROM tb_nilai WHERE id_mhs = 1) n ON m.id = n.id_mhs<br \/>\nJOIN tb_matakuliah mk ON n.id_matakuliah = mk.id<\/p><\/blockquote>\n<p>Note: the example query above can be done with a note that we only filter for one or several students.<\/p>\n<p>3. Update function by joining<\/p>\n<p>Sometimes we want to update by emptying the value in the table tb_value with the name of a certain student,<br \/>\nthen we can use update command with where link.<\/p>\n<blockquote><p>UPDATE tb_nilai n, tb_mhs m<br \/>\nSET n.nilai =0<br \/>\nWHERE n.id_mhs = m.id<br \/>\nAND m.id IN (1, 2, 3, 4)<\/p><\/blockquote>\n<p>4. Rarely used cross join function<\/p>\n<p>The tb_value table is a liaison table between the tb_mhs and tb_matalaku tables,<br \/>\nand we can generate table tb_value by using cross join command<\/p>\n<blockquote><p>INSERT INTO tb_nilai (id_mhs, id_matakuliah)<br \/>\nSELECT m.id, mk.id<br \/>\nFROM tb_mhs m<br \/>\nCROSS JOIN tb_matakuliah mk<\/p><\/blockquote>\n<p>In retail applications, cross joins are commonly used by authors to generate stock tables which are:<br \/>\nlink between product table and warehouse table<\/p>\n<blockquote><p>INSERT INTO tb_stok (id_produk, id_gudang)<br \/>\nSELECT m.id, g.id<br \/>\nFROM tb_produk m<br \/>\nCROSS JOIN tb_gudang g<\/p><\/blockquote>\n<p>5. Put the index in the right position<\/p>\n<p>Over time, the data will continue to grow, so it is necessary to put an index on the column that<br \/>\noften used for search or join linking keys.<br \/>\nRemember redundant indexes will slow down performance, use indexes sparingly.<br \/>\nWith the example table above, the index needs to be placed on the tb_value for the id_mhs column and also the id_course.<br \/>\nUsually an automatic index will be created for the column with the primary key attribute.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Use numeric data type as primary key or foreign key Often beginners will design tables using string types as keys, and this will result in future performance when the data is large, so&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1032,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,2,14],"tags":[11,7],"class_list":["post-818","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-komputer","category-recommended","tag-database","tag-sql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts\/818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/comments?post=818"}],"version-history":[{"count":1,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts\/818\/revisions"}],"predecessor-version":[{"id":4126,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/posts\/818\/revisions\/4126"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/media\/1032"}],"wp:attachment":[{"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/media?parent=818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/categories?post=818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bagi2info.com\/en\/wp-json\/wp\/v2\/tags?post=818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}