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 7823707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:20:43+00:00 2026-06-02T08:20:43+00:00

The statement of the problem is the following: -One has a table INIT with

  • 0

The statement of the problem is the following:

-One has a table INIT with structure

(number1 INT not null, number2 INT not null, ..., number7 INT not null)

-I want to insert into table ‘tab’ all rows of table INIT but I don’t want
to have 2 rows in ‘tab’ such that one is a permutation of the other. So, for example,
if (1,2,3,7,19,21,6) and (19,2,3,7,1,21,6) are rows in INIT, then one and only one
of them has to end up in ‘tab’. It doesn’t matter which of them ends up in ‘tab’.

-What my code below does is the following: I keep an auxiliary table ‘aux’ with
the same structure of INIT. I iterate over all rows of table INIT and for each row
in INIT I sort it in increasing order of its components, so if (1,2,3,7,19,21,6) is
a row in INIT, I sort it (1,2,3,6,7,19,21) and check if it is in ‘aux’. If it is
I continue to the next row. Else, I insert (1,2,3,7,19,21,6) in ‘tab’.

I ran this procedure over the table INIT that contains 300,000 rows and I estimate
that it takes over 7 hours to run. I would like to know how can I improve the
running time of this procedure.

DECLARE done BOOLEAN default 0;
DECLARE n1,n2,n3,n4,n5,n6,n7 INT;
DECLARE o1,o2,o3,o4,o5,o6,o7 INT;
DECLARE my_cursor cursor  FOR select * from INIT;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;       
OPEN my_cursor;

drop table if exists aux;
create table aux(
  number1 INT not null,
  number2 INT not null,
  number3 INT not null,
  number4 INT not null,
  number5 INT not null,
  number6 INT not null,
  number7 INT not null,
 );
 create table temp( number INT );

REPEAT
   truncate table temp;

   FETCH my_cursor INTO n1,n2,n3,n4,n5,n6,n7;
        INSERT INTO temp values(n1);
        INSERT INTO temp values(n2);    
        INSERT INTO temp values(n3);
        INSERT INTO temp values(n4);
        INSERT INTO temp values(n5);
        INSERT INTO temp values(n6);
        INSERT INTO temp values(n7);
        BEGIN
           DECLARE done2 BOOLEAN default 0;
           DECLARE my_cursor2 cursor  FOR select * from temp order by number;  
           OPEN my_cursor2;
             FETCH my_cursor2 INTO o1;
             FETCH my_cursor2 INTO o2;
             FETCH my_cursor2 INTO o3;
             FETCH my_cursor2 INTO o4;
             FETCH my_cursor2 INTO o5;
             FETCH my_cursor2 INTO o6;
             FETCH my_cursor2 INTO o7;

             IF NOT EXISTS (SELECT * FROM aux where number1=o1 AND number2=o2 AND number3=o3 
                            AND number4=o4 AND number5 = o5 AND number6 = o6 AND number7=o7 ) 
             THEN
                 INSERT INTO tab VALUES (n1,n2,n3,n4,n5,n6,n7);
             END IF;
           CLOSE my_cursor2;
         END;
UNTIL done END REPEAT;
CLOSE my_cursor;

EDITED:
-In each row of INIT, all integers are different.
-The primary key of INIT is (number1,number2,…,number7)

  • 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-02T08:20:45+00:00Added an answer on June 2, 2026 at 8:20 am

    You’re doing a heavy query for every row… not a good approach.

    Instead, you can use some database kung fu to get the job done without a stored proc:

    insert into tab
    select number1, number2, number3, number4, number5, number6, number7 from (
      select number1, number2, number3, number4, number5, number6, number7, 
        group_concat(number order by number) as sig from (
          select number1, number2, number3, number4, number5, number6, number7, number1 as number
          union all select number1, number2, number3, number4, number5, number6, number7, number2
          union all select number1, number2, number3, number4, number5, number6, number7, number3
          union all select number1, number2, number3, number4, number5, number6, number7, number4
          union all select number1, number2, number3, number4, number5, number6, number7, number5
          union all select number1, number2, number3, number4, number5, number6, number7, number6
          union all select number1, number2, number3, number4, number5, number6, number7, number7) a
    ) group by sig) b
    

    The key tricks involved here are:

    • the inner select allows group_concat to do the work of grouping up the numbers in a standard order so the combination can be compared
    • group_concat with order by gives you a unique signature for the numbers
    • using group by without aggregation in mysql gives you the first row for each group-by column value

    BTW, the correct term is combinations not permutations

    Also, I haven’t tested this, so there could be a misplaced bracket etc, but it should “basically” work

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem creating the following SQL Statement using LINQ & C# select
I have a complex sorting problem with my SQL statement. I have a table
I want to transfer the following statement to SubSonic 2.2 SELECT b.* FROM tableA
A colleague of mine has a problem with a sql query:- Take the following
I am trying to solve the following codechef problem using scala. The statement of
Problem Statement: I'm creating a template for multi tiered complicated calculations in MS Excel
Problem statement: It is necessary for me to write a code, whether which before
Here is the problem statement: Calling a setter on the object should result in
I have a problem with a continue statement in my C# Foreach loop. I
I have a problem with the SQL statement detailed below. The query returns the

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.