473,595 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query Performance Improvement

Seth Schrock
2,965 Recognized Expert Specialist
I've got a query that I'm using to track if certain tasks are completed. A task is completed based on different fields being populated. I have a table that lists which field to look for. My current solution is to use a public function to determine which field to look for and then check if there is a value in that field. The only issue is that using a function for a bunch of records decreases the performance. I'm hoping that someone knows some trick to be able to do it via SQL.

For some examples, let say I have a table of customers (tblCustomers) with several fields
Expand|Select|Wrap|Line Numbers
  1. tblCustomers
  2. CustomerName
  3. BirthDate
  4. SSN
  5. FavoriteColor
Then we have a table listing all the tasks and then the field name to look into for a value
Expand|Select|Wrap|Line Numbers
  1. tblTasks
  2. TaskDescription                           TaskField
  3. Name Entered                              CustomerName
  4. Birthday Provided                         BirthDate
  5. SSN Entered                               SSN
  6. Favorite Color Provided                   FavoriteColor
Is there a way for SQL to know to look in CustomerName for the task Name Entered? It is easy with VBA, but I can't think how to do it in SQL.
Jul 9 '21 #1
15 4101
NeoPa
32,566 Recognized Expert Moderator MVP
Hi Seth.

Your explanation is not good but I can see you put effort into it so I'll offer what I can.

Would I be correct to guess that a record in your [tblTasks] table might have a [TaskDescription] value of the string "Birthday Provided" and a [TaskField] value of "BirthDate" to reflect that's the field to be matched when filtering? Thus you would find all customers from [tblCustomers] where the [BirthDate] field matches some date value you've not told us about yet?

I have to say the more I look at this the more weird the design appears to be. Maybe I just don't understand (Not too surprising in the circumstances in spite of your efforts) but this just seems strange to me.

Maybe if you complete the question it might make more sense.
Jul 10 '21 #2
cactusdata
214 Recognized Expert New Member
You could start with creating a query that lists the status of the records:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     CustomerName Is Not Null As [Name Entered],
  3.     BirthDate Is Not Null As [Birthday Provided],
  4.     SSN Is Not Null As [SSN Entered],
  5.     FavoriteColor Is Not Null As [Favorite Color Provided]
  6. FROM 
  7.     tblCustomers
Then you can filter on that as you like.
Jul 11 '21 #3
isladogs
457 Recognized Expert Moderator Contributor
I'm also somewhat perplexed by the point of this but you could just do something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT Nz([CustomerName],"") AS [Name Entered], Nz([BirthDate],Null) AS [Birthday Provided], 
  2. Nz([SSN],"") AS [SSN Entered], Nz([FavoriteColor],"") AS [Favorite Colour Provided]
  3. FROM tblCustomers;
The difference between the 2 approaches is that using @cactusdata method, you will get a series of boolean values.
Using Nz, you will get actual values where they exist.

BUT in fact you will get EXACTLY the same output just by aliasing each field in your query

Expand|Select|Wrap|Line Numbers
  1. SELECT CustomerName AS [Name Entered],BirthDate AS [Birthday Provided], 
  2. SSN AS [SSN Entered], FavoriteColor AS [Favorite Colour Provided]
  3. FROM tblCustomers;
EDIT:
Once you have done either of the above, you can then easily enter data in tblTasks according to whatever rules you have.
BUT if you used the same field names in both tables, none of the above would be necessary as the tables could easily be linked.
Having said all of that I'm not sure I see the purpose of tblTasks as written. If it only has those fields it seems to be pointless to me.
If it really is needed, why not add an autonumber CustID field to the first table and the same field to the second table as a foreign key.
You can then easily link the 2 tables and the other fields in tblTasks can be deleted. Queries can then be used to join the 2 tables as required

Perhaps I'm missing something in your post that would indicate why none of those would be valid approaches for you
Jul 11 '21 #4
Rabbit
12,516 Recognized Expert Moderator MVP
If the number of actual fields is managable, you can "unpivot" the main table using a view with a series of union all. This will allow you to join the unpivoted view to the task table.
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     ID,
  3.     'Field1' as FieldName,
  4.     Field1 as FieldValue
  5. FROM Table
  6.  
  7. UNION ALL 
  8.  
  9. SELECT
  10.     ID,
  11.     'Field2' as FieldName,
  12.     Field2 as FieldValue
  13. FROM Table
  14.  
  15. UNION ALL
  16.  
  17. ...
Jul 11 '21 #5
Seth Schrock
2,965 Recognized Expert Specialist
My actual process is very complex, so I tried to create a simple version to get to the heart of my question. However, it seems I left out some details in my quest for simplicity. I'm going to continue with my simple version, but try to provide some background. There are different types of customers, each having their own checklist. The tasks on the checklist vary in number and description. I also want it to be flexible, so that if a process changes sometime in the future (a somewhat common occurance), I don't have to change/add fields, fix forms, queries, reports, etc., but can just add or remove a record - hence the need for [tblTasks]. [TaskDescription] is the human readable text the user will see as the label for the task. [TaskField] is the field that the system needs to look in to know if the task has been completed.

@NeoPa
You are correct about the [tblTasks] having the two fields (among others) [TaskDescription] and [TaskField] having those strings as multiple records. However, I'm not matching [tblCustomers] values to "some date value", but just to the fact that there is a value. So for each customer in [tblCustomers], there is a checklist of all the tasks (listed in [tblTasks]) with a checkmark that data is provided or not (data validation happening on the entry side, not the checklist side. So if the checklist sees data, it assumes it is valid data.). The reason for having a separate table for the checklist is that there are different categories of customers, each with their own checklist and I also want the list to be changeable without a redesign of the database. My goal (not sure if its possible) is to somehow use the value in TaskField to pull that field from [tblCustomers] using SQL instead of a public function. Rabbit's suggestion would allow me to do that, but isn't as flexible as I was hoping for. Using VBA, it is just as simple as Recordset.Field s(FieldName) allowing you to use a variable to get the value from any field that you want in the recordset. That type of thing is what I've got in my mind.

@cactusdata
My goal is to have each task listed as a separate record and not all the tasks as fields of one record.

@isladogs
I'm actually just wanting boolean values to represent data present or not. For the purpose of my checklist, I don't care what the value is. I think I may have caused some confusion with my depiction of [tblTasks]. The field names are in bold and the lines below are records with the applicable data under each field name. This allows me to have each task as a separate record, instead of separate fields of the same record.

@Rabbit
My actual checklist has up to 33 items on it. Let me see if I'm understanding your solution correctly. I would have each individual field in [tblCustomers] in a separate query and then UNION them all together? Such as the following:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.      ID,
  3.      'CustomerName' As FieldName,
  4.      CustomerName As FieldValue
  5. FROM tblCustomers
  6.  
  7. UNION ALL
  8.  
  9. SELECT
  10.      ID,
  11.      'BirthDate' As FieldName,
  12.      BirthDate As FieldValue
  13. FROM tblCustomers
  14.  
  15. ...
This would allow me to join to [tblTasks] on the FieldName field.
Jul 12 '21 #6
cactusdata
214 Recognized Expert New Member
use a variable to get the value from any field that you want in the recordset. That type of thing is what I've got in my mind.

Field and table names in SQL must be static, so you can't.
Stay with VBA and DAO, indeed as it is so simple to implement this way.
Jul 12 '21 #7
isladogs
457 Recognized Expert Moderator Contributor
I agree. Your description of tblTasks was anything but clear.
Having read your clarification, I agree with what @cactusdata is saying.
Jul 12 '21 #8
Rabbit
12,516 Recognized Expert Moderator MVP
My actual checklist has up to 33 items on it. Let me see if I'm understanding your solution correctly. I would have each individual field in [tblCustomers] in a separate query and then UNION them all together?
Yes, your words don't quite match but the sample query you sent is what I meant
Jul 12 '21 #9
NeoPa
32,566 Recognized Expert Moderator MVP
Hi Seth.

It's often very difficult to explain a situation that's very complex. It shows consideration to try to simplify it, and I have to applaud you for that, however that also often leads to a mess. I think it fair to say you fell into that category on this one. I can sympathise, and still applaud you for the attempt.

CactusData:
Field and table names in SQL must be static, so you can't.
Stay with VBA and DAO, indeed as it is so simple to implement this way.
I can't disagree with this but I might want to differentiate between using DAO Recordsets in VBA and using VBA to adjust SQL stored somewhere (Even in QueryDefs.) so that it matches your requirements. After all SQL strings within VBA are just strings.

Clearly though, in case you weren't aware before, SQL syntax doesn't allow the use of variable data in references (Tables; Fields; etc).

Now, I can see that your explanations now are a lot cleaner and clearer (which is great) but if you wouldn't mind giving me the resultant SQL you're after, as examples, for just the first two scenarios in your list - Name & Birth Date Fields - and then I think I can be more confident I actually follow what you're after. Forget using any flexibility in either SQL string and you probably won't even need to refer to the table [tblTasks], just post the SQL that does what you want it to. Feel free to use SELECT [Table].* in this even if you wouldn't want to do that in your actual work. I'm not too interested in which Fields are being shown - just the logic ;-)
Jul 12 '21 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1742
by: inna | last post by:
Hello. I have query performance question. I need to optimize procedure CREATE PROCEDURE dbo.SECUQUSRCOMPACCES @P1 VARCHAR(50), @P2 INTEGER AS DECLARE @IORGANIZATIONID INTEGER EXECUTE dbo.ORGNQGETORGID @PORGUNIQUEID = @IORGANIZATIONID OUTPUT
8
3244
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run simultaneously 4 similar queries it takes nearly 5 minutes instead of 4 times 9 seconds or something near of that. here is a sample query: select mertido, fomeazon, ertektipus, mertertek from t_me30 where fomeazon in (select distinct fomeazon...
1
1890
by: Peter Arrenbrecht Opus | last post by:
Hello IBM I think that one could improve the performance of DB2 UDB v7.2's stored procedure resolution. Here's what DB2 normally does: SELECT A.PROCSCHEMA, A.PROCNAME, A.PARMNAME, A.TYPESCHEMA, A.TYPENAME, A.LENGTH, A.SCALE, A.PARM_MODE, A.ORDINAL, A.CODEPAGE FROM "SYSIBM".SYSPROCPARMS A, "SYSIBM".SYSPROCEDURES B WHERE A.PROCSCHEMA = 'UMBNT'AND A.PROCNAME =
10
1879
by: Gary | last post by:
I would like to know if I use C# instead of Vb.Net for Server components, will there a performance improvement ? TIA, Gary
9
1577
by: wdwedw | last post by:
I have included all the source codes in the attached MyTest.zip (http://www.codeguru.com/forum/attachment.php?attachmentid=11218) There are three projects: VBTestCOM project is a apartment threaded DLL, it has one function doing a stored procedure call. This DLL will be called from C++ multithread. C++ test project is a ATL multithreaded DLL, it just simple created multithread, and call VBTestCom's doSPCall function in each thread.
10
1853
by: varlagas | last post by:
I execute a query (against DB2 for iSeries), which, in its generic form is as follows. This query runs just fine, executing in a couple of seconds SELECT V.FIELD01, V.FIELD02, V.FIELD03, V.FIELD04, V.FIELD05, V.FIELD06, V.FIELD07, V.FIELD08, V.FIELD09, V.FIELD10, V.FIELD11, V.FIELD12, V.FIELD13 FROM SCHEMA1.VIEW1 V WHERE V.FIELD2 BETWEEN '03/10/2005' AND '03/10/2006' AND (V.FIELD4 = '103' OR V.FIELD4 = '100' )
9
2112
by: db2udbgirl | last post by:
Is this possible to tune this query further : Env : DB2 UDB 8.2 on AIX 5.3, Non partitioned tables Query: SELECT ETL.T00601.* FROM ETL.T00601, ETL.STG_LAAM_CARD20_BUS_ASCT_BUS_FCN where
4
5092
by: Sam Durai | last post by:
Hello, I have a requirement to delete millions of rows from a table which has billions of rows. Hence I have coded a korn script to delete rows recursively and with a commit interval of 10000. However I feel that the delete is taking some time to delete, hence would it be a good idea to code a stored procedure to implement the same ? If you have a similar exp can you please share your thoughts. Here is the korn script which I'm using...
2
2416
by: Brian Tabios | last post by:
Hello Everyone, I have a very complex performance issue with our production database. Here's the scenario. We have a production webserver server and a development web server. Both are running SQL Server 2000. I encounted various performance issues with the production server with a particular query. It would take approximately 22 seconds to return 100 rows, thats about 0.22 seconds per row. Note: I ran the query in single user mode. So...
5
3832
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big table, we try separate this big table into twelve tables and create a view
0
8261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8019
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8251
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6674
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5418
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3873
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.