query-optimization

Retail inventory system Mysql query optimization

帅比萌擦擦* 提交于 2019-12-11 09:41:26
问题 I'm looking to optimize the following Mysql query that is used to calculate the cost of inventory for a number of stores, I'm running the following query through a PHP loop for each distinct store and outputting the result: Query: SELECT SUM(((( (SELECT COALESCE(SUM(facturas_fabrica.cantidad), 0) FROM facturas_fabrica INNER JOIN entradas_pedidos_productos ON entradas_pedidos_productos.clave = facturas_fabrica.entradas_pedidos_productos_clave INNER JOIN entradas_pedidos ON entradas_pedidos

Lightweight in-memory database

不羁岁月 提交于 2019-12-11 08:39:41
问题 One of our needs is to create an temporal in-memory database, to then perform various inserts/selects/updates. At a glance SQLite satisfied all our needs. Connection to an in-memory SQLite DB can be established as simple as: class SQLiteBase < ActiveRecord::Base self.abstract_class = true establish_connection(adapter: 'sqlite3', database: ':memory:') end A while ago we've started looking into some performace issues when it turned out that we need to perform bulk-loading (specifically, bulk

What options do I have to make my ORDER BY faster?

喜你入骨 提交于 2019-12-11 07:38:40
问题 I have the following query: SELECT DISTINCT c.id FROM clients AS c LEFT JOIN client_project AS cp ON (cp.client_id = c.id) WHERE cp.project_id = 1 AND c.active_flag = 1 ORDER BY c.client_name If I remove the order by, the query takes 0.005 seconds. With the order by, the query takes 1.8-1.9 seconds. I have an index on client_name . What else would improve the speed? Edit: c.id is primary key, but there could be multiple records for it in client_project and therefore it may result in more than

Problems to optimize large query and tables structure

六月ゝ 毕业季﹏ 提交于 2019-12-11 07:07:48
问题 I'm not an DB expert, so I've been around for a while, reading as much as I can and thanks to the community answers I could make several changes to my query and tables structure. Even after reading a lot of stuff I got stuck, so I came to make my first question. I have a website where the users post their own stories. Each story can have genres, warnings, multiple authors, multiple characters assigned, etc. We're running MySQL 5.x, tables are InnoDB, website written in PHP. Using GROUP_CONCAT

Help Me with my SQL Query (Need it to always return at least one row.)

为君一笑 提交于 2019-12-11 06:58:33
问题 First off, here's my SQL query: SELECT research_cost, tech_name, (SELECT research_cost FROM technologies WHERE research_cost <= USERS_RESEARCH_POINTS_VALUE ORDER BY research_cost DESC LIMIT 1) as research_prev, (SELECT cost FROM technology_costs WHERE id = 18 LIMIT 1) as technology_cost FROM `technologies` JOIN technology_costs ON id = COUNT_OF_TECHS_USER_LEARNED WHERE research_cost > USERS_RESEARCH_POINTS_VALUE ORDER BY research_cost ASC LIMIT 1 Website link: http://www.joemajewski.com

Linq2SQL: Optimize query?

扶醉桌前 提交于 2019-12-11 06:33:45
问题 Can I optimized the following query in any way: The goal is to try to find clients of type 4 that also exists as type2 based on their VAT and Email. A client can have one or more clientusers. (I have all the appropriate indexes set, I promise) from e in Clients.Where(h => h.ClientTypeId==4) join u in ClientUsers on e.Id equals u.ClientId where e.DeleteFlag.Equals("n") && ( (!(e.VAT == null || e.VAT.Equals("")) && Clients.Any(f => f.ClientTypeId == 2 && f.VAT.Equals(e.VAT))) || (!(e.Email ==

Query optimization: max() in subquery

萝らか妹 提交于 2019-12-11 05:05:19
问题 select active from websites where id = (select max(id) from websites where url = 'google.com') id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY websites const PRIMARY PRIMARY 4 const 1 2 SUBQUERY websites ref url url 767 1867 Using where How can I optimize this query? The url field is index and id is the primary key. So why is it going through all the rows? 回答1: MAX always process all rows - use order by and limit - so query will look this way SELECT * FROM wbsites

MySQL complex subquery formulation

给你一囗甜甜゛ 提交于 2019-12-11 04:57:45
问题 I have two tables - books and images . books has columns like id , name , releasedate , purchasecount . images has bookid (which is same as the id in books, basically one book can have multiple images. Although I haven't set any foreign key constraint), bucketid , poster (each record points to an image file in a certain bucket, for a certain bookid ). Table schema: poster is unique in images , hence it is a primary key. Covering index on books: ( name , id , releasedate ) Covering index on

Date of max id: sql/oracle optimization

本小妞迷上赌 提交于 2019-12-11 04:36:18
问题 What is a more elegant way of doing this: select date from table where id in ( select max(id) from table); Surely there is a better way... 回答1: select date from (select date from table order by id desc) where rownum < 2 assuming your ids are unique. EDIT : using subquery + rownum 回答2: You can use the ROWNUM pseudocolumn. The subquery is necessary to order the result before finding the first row: SELECT date FROM (SELECT * FROM table ORDER BY id DESC) WHERE ROWNUM = 1; You can use subquery

When I ORDER BY a computed column, the query slows significantly - Can this be sped up?

痞子三分冷 提交于 2019-12-11 04:28:13
问题 The following query is taking more than 25 seconds to complete: SELECT c.* , (c.age+ (UNIX_TIMESTAMP()-UNIX_TIMESTAMP(c.newdate))) AS ranking , IF(uc.id_user = 7,1,0) AS favorite FROM c LEFT OUTER JOIN uc ON uc.id_cluster = c.id AND uc.id_user = '7' LEFT OUTER JOIN d ON d.id_cluster = c.id LEFT OUTER JOIN dt0 ON dt0.id_document = d.id LEFT OUTER JOIN t0 ON dt0.id_term = t0.id WHERE MATCH(t0.normalizacion) AGAINST ('term' IN NATURAL LANGUAGE MODE) GROUP BY c.id ORDER BY ranking ASC LIMIT 30