query-optimization

Join multiple tables by multiple grouping

守給你的承諾、 提交于 2019-12-06 15:38:50
We have a passing control system and every pass action is stored Event table in MSSQL Server . We want to join multiple tables with the Event table according to their relations as shown on the image below. However, I am not sure if the grouping approach that I used is correct or not because the query takes a lot of time. Could you please clarify me oh how to join these tables by multiple grouping? Here is the JOIN clause I used: SELECT t.CardNo, t.EventTime, t1.EmployeeName, t1.Status, t2.EventCH, t3.DoorName, t4.JobName, t5.DeptName FROM Event t LEFT JOIN Employee AS t1 ON t.EmployeeID = t1

Does MySQL use existing indexes on creating new indexes?

霸气de小男生 提交于 2019-12-06 13:36:28
I have a large table with millions of records. Table `price` ------------ id product site value The table is brand new, and there are no indexes created. I then issued a request for new index creation with the following query: CREATE INDEX ix_price_site_product_value_id ON price (site, product, value, id); This took long long time, last time I was checking ran for 5000+ seconds, because of the machine. I am wondering if I issue another index creation, will it use the existing index in the process calculation? If so in what form? Next to run query 1: CREATE INDEX ix_price_product_value_id ON

how to optimize mysql query if i have too many OR operators in my query?

三世轮回 提交于 2019-12-06 13:29:38
问题 using PHP and MySQL i have grabbed an array of facebook user ids from facebook. Now i want to find the corresponding username in my application for this array. Clearly in my application the user table contains unique username and unique fb_uid values. my rudimentary understanding oof programming led me to 2 ways: 1) use a loop and run through the array of fb_uid and find the username one by one. OR 2) create a monster query like select distinct(username) from users where fb_uid = value1 OR fb

How to optimize huge query with repeated subqueries

寵の児 提交于 2019-12-06 12:25:41
I have the following huge query that contains repeated subqueries , It looks really inefficient to me. How can i optimize it ? SELECT T2.date1, T2.date2, T2.period, T1.market, T1.ticker, 0 AS scenario FROM (SELECT DISTINCT Q.market AS market, Q.ticker AS ticker FROM portfolio.scenario S RIGHT JOIN portfolio.quote Q ON S.series = (SELECT S.series FROM scenario S WHERE S.date1 >= '2009-09-01' AND S.date2 <= '2010-07-01' AND S.period = 'QUARTER' ORDER BY S.date2 LIMIT 1) AND Q.market = S.market AND Q.ticker = S.ticker WHERE Q.date = '2010-07-01' AND S.date1 IS NULL) AS T1 JOIN (SELECT DISTINCT S

Nested Set indices & performance

北城余情 提交于 2019-12-06 11:37:08
I'm having some troubles understanding what indices to use on a Nested-Set model. The query is: SELECT `node`.`id`,(COUNT(parent.id) - 1) AS `depth`,`name` FROM `categories` AS `parent` INNER JOIN `categories` AS `node` ON (`node`.`lft` BETWEEN parent.lft AND parent.rgt) INNER JOIN `filebank_categories` ON (`node`.`id` = `filebank_categories`.`category_id` AND `filebank_categories`.`filebank_id` = 136) INNER JOIN `categories_names` ON (`categories_names`.`category_id` = `node`.`id` AND `categories_names`.`language_id` = 1) WHERE `node`.`system_id` = parent.system_id GROUP BY node.id ORDER BY

Why does Grails issue SELECT statements on instance save()?

北城以北 提交于 2019-12-06 11:24:57
Each time I save() a new domain object in Grails I see Hibernate issuing Select statements for each of the unique constraints in my Grails domain class. If I save without validation, I don't see the Select statements. Example: new Person(name: 'Foo').save(validate: false) Is there a way to stop these extra Select statements from occurring while still having validation enabled? One of my goals is to decrease appserver/database chattiness since I am expecting my Grails app to run on a high latency network (e.g., communicating from Grails app => DB can be costly). Burt has one suggestion, but I'm

How to select many to one to many without hundreds of queries using Django ORM?

只愿长相守 提交于 2019-12-06 10:12:44
My database has the following schema: class Product(models.Model): pass class Tag(models.Model): product = models.ForeignKey(Product) attr1 = models.CharField() attr2 = models.CharField() attr3 = models.CharField() class AlternatePartNumber(models.Model): product = models.ForeignKey(Product) In other words, a Product has many Tag s, and a Product has many AlternatePartNumber s. Tag s are a collection of attributes of the Product . Given the three attributes in a Tag , I want to select the associated Product s that match (could be more than one), as well as all of the AlternatePartNumber s of

MYSQL and the LIMIT clause

ぃ、小莉子 提交于 2019-12-06 10:09:22
I was wondering if adding a LIMIT 1 to a query would speed up the processing? For example... I have a query that will most of the time return 1 result, but will occasionaly return 10's, 100's or even 1000's of records. But I will only ever want the first record. Would the limit 1 speed things up or make no difference? I know I could use GROUP BY to return 1 result but that would just add more computation. Any thoughts gladly accepted! Thanks It depends if you have an ORDER BY. An ORDER BY needs the entire result set anyway, so it can be ordered. If you don't have any ORDER BY it should run

Speed up Django & Postgres with simple JSON field

眉间皱痕 提交于 2019-12-06 08:50:18
I have a very very complex model with lots of related models by FK and M2M which are also have lots of relations, etc. So, rendering a list of such objects is a very expensive SQL operation, and i want to optimise it. (select_related and prefetch_related help, but a little) I have maybe a very stupid but very simple idea - define save method, that will serialize all object's data to a field stores JSON To do something like this: class VeryComplexModel(models.Model): # some_field # some_field # ... json = models.TextField() def save(self): json = serialize(self) in views.py: complexModels =

How to optimise the 'XQuery' SQL

青春壹個敷衍的年華 提交于 2019-12-06 07:48:17
问题 I have an XML hierarchy like this in an XML type column of a table with 10,000 records- <Root> <Elem1> <Parent1> <Separator> <Child1/> </Separator> </Parent1> </Elem1> </Root> I have a query like this - DECLARE @Root VARCHAR(50) DECLARE @Entity VARCHAR(50) DECLARE @ParentNode VARCHAR(50) DECLARE @Separator VARCHAR(50) DECLARE @ChildNode VARCHAR(50) SET @Root = 'Root' SET @Entity = 'Elem1' SET @ParentNode = 'Parent1' SET @Separator = 'separator' SET @ChildNode = 'Child1' select Parent.P.value(