Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6115581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:06:28+00:00 2026-05-23T15:06:28+00:00

Running Postres 7.4 (yep upgrading) This is a view query and it’s used a

  • 0

Running Postres 7.4 (yep upgrading)

This is a view query and it’s used a lot but takes longer than I would like. Any Optimization Suggestions? Any field with a Id in the name is indexed

SELECT  
db_tbl_1.field_id, s."Field ID" AS foreign_field_id,
s."Field Name" AS field_name,
CASE    
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '01' THEN 'Field Label 1'
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 4) = '321' THEN 'Field Label 1' 
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 5) = '1234' THEN 'Field Label 1'
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 6) = '55555' THEN 'Field Label 1'    
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '76' THEN 'Field Label 2' 
END AS new_field, s.field_1,
db_tbl_2.field_2, db_tbl_1.field_2_a, 
db_tbl_1.field_3, db_tbl_1.field_4,
db_tbl_1.field_5, db_tbl_1.field_6, db_tbl_1.field_date_1,
db_tbl_1.field_7, db_tbl_2.field_8 AS new_field_name,
s."Field Date" AS field_date, db_tbl_1.created_date,
CASE
    WHEN (((DATE_TRUNC('month', "Field Date") + INTERVAL '2 MONTH') - "Field Date" > 60) AND 
         ((DATE_TRUNC('month', "Field Date") + INTERVAL '2 MONTH') - "Field Date" <= 92))
    THEN (DATE_TRUNC('month', "Field Date") + INTERVAL '2 MONTH')
    -- the most days in a three month span can be 92
    ELSE (DATE_TRUNC('month', "Field Date") + INTERVAL '3 MONTH')
END AS next_date
FROM db_schema_1.db_tbl_1, db_schema_1.db_tbl_2, "Table S" AS s
WHERE db_tbl_1.program_level_id = db_tbl_2.id
AND db_tbl_1.field_id = s."Another ID"
AND (CASE   
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '01' THEN 1
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 4) = '321' THEN 1 
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 5) = '1234' THEN 1
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 6) = '55555' THEN 1  
    WHEN SUBSTRING(s."Field ID" FROM 0 FOR 3) = '76' THEN 1
    ELSE 0 
END) = 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:06:29+00:00Added an answer on May 23, 2026 at 3:06 pm

    Try to change the last statement, where you are using the case statement because the query will evaluate all of them. and then check if is equals to 1, you should try and “OR” statement, because if one is correct, it will not evaluate the other alternatives. It should help you a bit. Something like this.

    AND ( SUBSTRING(s."Field ID" FROM 0 FOR 3) = '01' OR
        SUBSTRING(s."Field ID" FROM 0 FOR 4) = '321' OR
        SUBSTRING(s."Field ID" FROM 0 FOR 5) = '1234' OR
        SUBSTRING(s."Field ID" FROM 0 FOR 6) = '55555' OR
        SUBSTRING(s."Field ID" FROM 0 FOR 3) = '76'
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in CakePHP how would go about running this query? It's to get the next
I currently have the query running on Postgres: SELECT * FROM addenda.users WHERE users.username
Running FxCop on my code, I get this warning: Microsoft.Maintainability : 'FooBar.ctor is coupled
Running OS X Leopard an MacBook Pro from Jan. 2008. I used to run
Running a ServiceHost with a single contract is working fine like this: servicehost =
Running ipconfig /all shows a Teredo Tunneling Pseudo-Interface. What is that? Does this have
I have a query that runs a lot slower (~5 minutes) when I run
We are running about in our java code with GregorianCalendar . We would like
I'm running a MySQL database locally for development, but deploying to Heroku which uses
We'll do this on almost every hit to our site. Hosted at Heroku, running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.