Answered by:
Query string compression
Question

User1777897341 posted
I'm working on an android application that will collect information from my mySQL database. For obvious security reasons I can't connect to the database with the application so instead I'm using an XML page to gather the information.
The problem is the search filters and having to pass potentially a lot of information through a query string. I found out a pretty ingenious method to significant shrink the size of the query string, but my question is, is this method too expensive?
Method
Each item on the database has a primary key which starts at 1 and so on. Well using math if you take each number to the power of 2 you can simply add them up together to compress the values.
Example: You want to reference IDs 2,3,5,7,8. Instead of sending the query string: ?keys=2,3,5,7,8 you take each number and take it to the power of 2 and add them.
2^2+2^3+2^5+2^7+8^2 = 428
The issue is having to decode the information on the server through a while loop.
Dim i as Integer = 428 'Total of 2^2+2^3+2^5+2^7+8^2 Dim x = 8 'Highest key in database in example Dim queueList As New Queue While i > 0 if i <= Math.Pow(2,x) queueList.Enqueue(x.toString()) i = i  (Math.Pow(2,x) end if x=x1 End While
Note: I wrote that on the spot and haven't tested it yet.
Anyway the question is, do you think this is too expensive to compute? Since it's a lot of looping once you get up to 100200 values. I don't expect to go higher than 200. Unless someone else knows a quicker way to do this without having to depend on a massive while loop running the Math.Pow function.
Thanks in advance
Monday, November 1, 2010 2:15 AM
Answers

User990694832 posted
Yes to expensive,
A savings would be to call the math.pow only once and store in variable.
DK
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 1, 2010 12:22 PM 
User1777897341 posted
Thanks DK, but I found the solution. It's been a while since I took a calc class in college.
Answer is simply Math.Floor(Math.log(value,2))
Math.log returns the highest possible value for 2^x=value along with a decimal. Simply use Math.Floor to remove the decimals and then use a while loop to solve.
A LOT less expensive. And of course as you mentioned use a variable to hold the Math.Floor/Math.log functions.
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 2, 2010 6:45 PM
All replies

User990694832 posted
Yes to expensive,
A savings would be to call the math.pow only once and store in variable.
DK
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 1, 2010 12:22 PM 
User1777897341 posted
Thanks DK, but I found the solution. It's been a while since I took a calc class in college.
Answer is simply Math.Floor(Math.log(value,2))
Math.log returns the highest possible value for 2^x=value along with a decimal. Simply use Math.Floor to remove the decimals and then use a while loop to solve.
A LOT less expensive. And of course as you mentioned use a variable to hold the Math.Floor/Math.log functions.
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 2, 2010 6:45 PM