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

  • SEARCH
  • Home
  • 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 8154773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:27:52+00:00 2026-06-06T16:27:52+00:00

Running this query takes a long time: SELECT host,ip FROM arecords WHERE ip IN

  • 0

Running this query takes a long time:

SELECT host,ip FROM arecords WHERE ip IN 
   (SELECT ip FROM arecords GROUP BY ip HAVING COUNT(ip)>1 )
;

(In my arecords table, sometimes there is more than one host on a IP. For these cases, I want to get the hostname+ip)

I have the feeling the sub query is run a lot of times. If I run the sub query, take the results, and replace the sub query with these results, it is fast. I found I can avoid this by using views.

Is there a way of doing it with one query?

[Example SQLfiddle of the result I’d like: http://sqlfiddle.com/#!2/6c1bd/1/0 ]

  • 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-06-06T16:27:53+00:00Added an answer on June 6, 2026 at 4:27 pm

    Try this solution. This will retrieve all rows for each ip that exists in the table more than once. This uses a subselect in FROM, which is executed once — rather than a subquery in a WHERE which gets executed once for each outer row.

    After the ips are selected, you join on the same table where the ip is equal to the ip that was selected in the FROM subselect. The join should be fast as it will happen over indexes and the final result is that it will get hosts + ips for only the ips having more than one value in the table:

    SELECT 
        b.host,
        b.ip
    FROM
        (
            SELECT ip
            FROM arecords
            GROUP BY ip
            HAVING COUNT(*) > 1
        ) a
    INNER JOIN
        arecords b ON a.ip = b.ip AND b.active = 'T'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After running this query statement with my database: select * from articles where content
I'm running this query: SELECT TOP 1 [DVD Copy].[Stock No] FROM [DVD Copy] WHERE
I am running this query which selects a few fields from one table and
I'm new to MySQL, and I'm running this query, SELECT item_id,amount FROM db.invoice_line WHERE
I am running into this Postgres issue where the same query takes a long
I'm running Mysql 5.0.77 and I'm pretty sure this query should work? SELECT *
I have an data transformation query which takes a long time to run on
I am trying to speed up a long running query that I have (takes
Here is my query: SELECT * FROM [GeoName] WHERE ((-26.3665122100029-Lat)*(-26.3665122100029-Lat))+((27.5978928658078-Long)*(27.5978928658078-Long)) < 0.005 ORDER BY
Due to performance issues arising from row locking and a long running query in

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.